Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 3, 2026

SourceGen XAML bindings like {Binding ViewModelProperty.Property} generated setters that throw NullReferenceException when intermediate properties are null at runtime—a valid scenario when data loads after page initialization.

Changes

  • CompiledBindingMarkup.cs: Replace previousPartIsNullable with previousPartIsReferenceType to wrap all intermediate reference type accesses in ConditionalAccess. XAML paths have no nullability annotations, so all reference types must be treated as potentially null.

  • Setter.cs: Handle value types inside ConditionalAccess when the next part is the final property to set. Structs need pattern matching capture to allow property assignment.

  • Tests: Update existing tests for new conditional access patterns; add regression test for the specific issue scenario.

Before

setter = static (source, value) =>
{
    source.ViewModelProperty.Property = value; // NullReferenceException
};

After

setter = static (source, value) =>
{
    if (source.ViewModelProperty is {} p0)
    {
        p0.Property = value;
    }
};
Original prompt

This section details on the original issue you should resolve

<issue_title>SourceGen NullReferenceException</issue_title>
<issue_description>### Description

When you have a binding

Property="{Binding ViewModelProperty.Property}"

and

<MauiXamlInflator>SourceGen</MauiXamlInflator>

it will generate

static global::Microsoft.Maui.Controls.BindingBase CreateTypedBindingFrom_bindingExtension94(global::Microsoft.Maui.Controls.Xaml.BindingExtension extension)
{
	global::System.Action<global::ViewModels.SomeViewModel, global::Microsoft.Maui.Graphics.Color>? setter = static (source, value) =>
	{
		source.ViewModelProperty.Property = value; // NullReferenceException is here because ViewModelProperty is null and it is ok for it to be null when data loads after the page appears. It doesn't make sense to create a Dummy object for every property that is set after setting binding context
	};

	return new global::Microsoft.Maui.Controls.Internals.TypedBinding<global::ViewModels.SomeViewModel, global::Microsoft.Maui.Graphics.Color>(
		getter: source => (source.ViewModelProperty.Property, true),
		setter,
		handlers: new global::System.Tuple<global::System.Func<global::ViewModels.SomeViewModel, object?>, string>[]
		{
			new(static source => source, "ViewModelProperty"),
			new(static source => source.ViewModelProperty, "Property"),
		});
}

So every time you launch the app you must tap continue on those exceptions that pop up which will be swallowed and will not crash the app but it is very annoying.

This should be handled in some normal way where null on ViewModelProperty is just ignored because it is a valid case for properties on VM. Not everything can be loaded before binding context is set

Steps to Reproduce

  1. Bind to control property Property="{Binding ViewModelProperty.Property}" while ViewModelProperty is some object and initially null.
  2. Turn on <MauiXamlInflator>SourceGen</MauiXamlInflator>
  3. Launch the app and navigate to the page with the binding

Expected: Null is ignored, no NullReferenceException is thrown even if it is swallowed/silenced
Actual: NullReferenceException is thrown as if null is not an expected valid value.

Link to public reproduction project repository

No response

Version with bug

10.0.30

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix NullReferenceException in SourceGen binding Fix SourceGen NullReferenceException for XAML bindings with null intermediate properties Feb 3, 2026
Copilot AI requested a review from simonrozsival February 3, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SourceGen NullReferenceException

2 participants