Description
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.
Metadata
Metadata
Assignees
Type
Projects
Status