Skip to content

NullReferenceException thrown at runtime when using OnPlatform with SourceGen enabled #34074

@APopatanasov

Description

@APopatanasov

Description

We’ve updated to SR4 and enabled SourceGen. The XAML compiled as expected with SourceGen enabled. However, a NullReferenceException is thrown at runtime. It appears to be caused by the OnPlatform XAML logic we have. When I add the following XAML:

<OnPlatform x:TypeArguments="View">
    <On Platform="WinUI">
        <Label Text="This is a label only visible on WinUI" />
    </On>
</OnPlatform>

the following exception is thrown:

System.NullReferenceException: Object reference not set to an instance of an object. at SourceGenRuntimeException.MainPage.InitializeComponent() in /Users/test/Desktop/SourceGenRuntimeException/obj/Generated/Microsoft.Maui.Controls.SourceGen/Microsoft.Maui.Controls.SourceGen.XamlGenerator/MainPage.xaml.xsg.cs:line 93
at SourceGenRuntimeException.MainPage..ctor() in /Users/test/Desktop/SourceGenRuntimeException/MainPage.xaml.cs:line 9
at SourceGenRuntimeException.App.CreateWindow(IActivationState activationState) in /Users/test/Desktop/SourceGenRuntimeException/App.xaml.cs:line 14
at Microsoft.Maui.Controls.Application.Microsoft.Maui.IApplication.CreateWindow(IActivationState activationState)

Steps to Reproduce

  1. Run the attached sample project.
  2. Compile the project and run it on a platform different from WinUI, as the OnPlatform example above targets WinUI. For instance, if the code is intended for Android, the issue will occur on all platforms except Android.

Link to public reproduction project repository

https://github.com/telerik/ms-samples/tree/main/Maui/SourceGenRuntimeException

Version with bug

10.0.40

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, Windows, macOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Labels

i/regressionThis issue described a confirmed regression on a currently supported versionpartner/syncfusionIssues / PR's with Syncfusion collaborationplatform/androidplatform/iosplatform/macosmacOS / Mac Catalystregressed-in-10.0.30s/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't workingxsgXaml sourceGen

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions