Description
When using SourceGen (MauiXamlInflator=SourceGen), a Setter with an {OnPlatform} value that doesn't specify a Default generates invalid C# code when building for a platform that doesn't match any of the specified platforms.
Steps to Reproduce
- Create a Style with a Setter using OnPlatform without Default:
<Style TargetType="Button">
<Setter Property="MinimumHeightRequest" Value="{OnPlatform Android=44}"/>
</Style>
- Enable SourceGen in the project:
<MauiXamlInflator>SourceGen</MauiXamlInflator>
- Build for a platform that doesn't match (e.g., MacCatalyst or iOS):
dotnet build -f net10.0-maccatalyst
Expected Behavior
The Setter should be skipped entirely when there's no matching platform value and no Default, or use a default value for the property type.
Actual Behavior
SourceGen generates invalid C# code that tries to call ProvideValue on a type instead of an instance:
var object0 = (object)((global::Microsoft.Maui.Controls.Xaml.IValueProvider)).ProvideValue(xamlServiceProvider);
This causes compilation errors:
CS0119: 'IValueProvider' is a type, which is not valid in the given context
CS0120: An object reference is required for the non-static member 'IValueProvider.ProvideValue(IServiceProvider)'
Workaround
Add a Default value to the OnPlatform expression:
<Setter Property="MinimumHeightRequest" Value="{OnPlatform Default=44, Android=44}"/>
Version with bug
- 10.0.30 (SR3)
- 10.0.40-dev (current main)
Link to public reproduction project repository
Reproduction available upon request (reported via support).
Relevant code
The issue is in how SimplifyOnPlatformVisitor removes the Value property from the Setter when OnPlatform has no matching platform, but the SourceGen code path doesn't handle this correctly - it still tries to generate code for the Setter's value but ends up with an empty/null variable reference.
Description
When using SourceGen (
MauiXamlInflator=SourceGen), aSetterwith an{OnPlatform}value that doesn't specify aDefaultgenerates invalid C# code when building for a platform that doesn't match any of the specified platforms.Steps to Reproduce
Expected Behavior
The Setter should be skipped entirely when there's no matching platform value and no Default, or use a default value for the property type.
Actual Behavior
SourceGen generates invalid C# code that tries to call
ProvideValueon a type instead of an instance:This causes compilation errors:
CS0119: 'IValueProvider' is a type, which is not valid in the given contextCS0120: An object reference is required for the non-static member 'IValueProvider.ProvideValue(IServiceProvider)'Workaround
Add a
Defaultvalue to the OnPlatform expression:Version with bug
Link to public reproduction project repository
Reproduction available upon request (reported via support).
Relevant code
The issue is in how
SimplifyOnPlatformVisitorremoves theValueproperty from the Setter when OnPlatform has no matching platform, but the SourceGen code path doesn't handle this correctly - it still tries to generate code for the Setter's value but ends up with an empty/null variable reference.