Skip to content

[XSG] Setter with OnPlatform without Default generates invalid code when platform doesn't match #33676

@StephaneDelcroix

Description

@StephaneDelcroix

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

  1. Create a Style with a Setter using OnPlatform without Default:
<Style TargetType="Button">
    <Setter Property="MinimumHeightRequest" Value="{OnPlatform Android=44}"/>
</Style>
  1. Enable SourceGen in the project:
<MauiXamlInflator>SourceGen</MauiXamlInflator>
  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-xamlXAML, CSS, Triggers, Behaviorsplatform/iosplatform/macosmacOS / Mac Catalysts/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triage

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions