Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XC] Consider compiling the OnIdiomExtension into OnIdiom<T> #22142

Open
simonrozsival opened this issue Apr 30, 2024 · 0 comments
Open

[XC] Consider compiling the OnIdiomExtension into OnIdiom<T> #22142

simonrozsival opened this issue Apr 30, 2024 · 0 comments
Labels
area-xaml XAML, CSS, Triggers, Behaviors

Comments

@simonrozsival
Copy link
Member

simonrozsival commented Apr 30, 2024

This is a follow-up to #21927 (see #21927 (comment)).

The {OnIdiom ...} XAML markup extension is not trimmable because it needs to call Activator.CreateInstance(type) with a type that cannot be annotated with [DynamicallyAccessedMembers].

In the cases when XamlC can infer the type of the value the extension is holding (T), the non-generic {OnIdiom ...} extension can be transformed into a generic instance of OnIdiom<T> which is trimming friendly. We can also consider producing warnings for the cases when the compiler is not able to infer T and prompt the developer to transform the extension into OnIdiom<T> manually.

Example 1

Consider this example:

<Label Text="{OnIdiom Default='Default Idiom', Phone='Phone', Tablet='Tablet', Desktop='Desktop', TV='TV', Watch='Watch'}" />

In this case, the compiler should be able to tell that all the values are strings and that the target property type is also a string. The transformed XAML should look like this:

<Label>
  <Label.Text>
    <OnIdiom x:TypeArguments="x:String">
        <OnIdiom.Default>Default Idiom</OnIdiom.Default>
        <!-- ... -->
    </OnIdiom>
  </Label.Text>
</Label>

Example 2

In the following example (src/ProfiledAot/src/maui/App.xaml), there is a mismatch between the target property type and the type of the OnIdiom default value:

<CollectionView Margin="{OnIdiom Default=1,1,1,1}" />

The compiler should be able to tell that the Margin property is of type Thickness which has a [TypeConverter(...)] attribute and so it can generate the IL to construct OnIdiom<Thickness> and use the TypeConverter to convert the string values into the target type.

/cc @StephaneDelcroix

@simonrozsival simonrozsival added the area-xaml XAML, CSS, Triggers, Behaviors label Apr 30, 2024
@PureWeen PureWeen added this to the .NET 9 Planning milestone May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML, CSS, Triggers, Behaviors
Projects
None yet
Development

No branches or pull requests

2 participants