Description
Control -> Handler Mapping
|
public static IMauiHandlersCollection AddMauiControlsHandlers(this IMauiHandlersCollection handlersCollection) |
|
{ |
|
handlersCollection.AddHandler<CollectionView, CollectionViewHandler>(); |
|
handlersCollection.AddHandler<CarouselView, CarouselViewHandler>(); |
|
handlersCollection.AddHandler<Application, ApplicationHandler>(); |
|
handlersCollection.AddHandler<ActivityIndicator, ActivityIndicatorHandler>(); |
|
handlersCollection.AddHandler<BoxView, ShapeViewHandler>(); |
|
handlersCollection.AddHandler<Button, ButtonHandler>(); |
|
handlersCollection.AddHandler<CheckBox, CheckBoxHandler>(); |
|
handlersCollection.AddHandler<DatePicker, DatePickerHandler>(); |
|
handlersCollection.AddHandler<Editor, EditorHandler>(); |
We have this massive list of direct references to XAML controls to handlers.
We could use something like this: #7258
Linking
Initially from #5028 (comment)
The issue is that Xaml is a form of code that the trimmer can't see. It can reference Types, and since the trimmer can't see which Types it uses, the trimmer doesn't know it needs to keep those Types.
I can think of the following options to resolve this:
-
Don't mark Microsoft.Maui.Controls as "Trimmable".
- We can still take some of the other changes here to resolve trimmer warnings, and possibly trim
Microsoft.Maui.dll. This would be at least a step in the right direction.
-
Take this change as is. And if someone is using LoadFromXaml in their app, instruct them to add <TrimmerRootAssembly Include="Microsoft.Maui.Controls" /> to their .csproj. This will instruct the trimmer to preserve the entire assembly. That way people who don't use LoadFromXaml can save the size off their apps. And people who do use it can make it work.
-
We could add a [DynamicDependency] on the LoadFromXaml API. This way, if this method is used in the app, the Types can be preserved. However, there are the following drawbacks with this approach:
- We would really want to say that all types in the Microsoft.Maui.Controls assembly be preserved. That way we wouldn't have to list each Type that could be used in Xaml. However, the trimmer doesn't have this feature (yet). We would have to ask for it to be implemented to say
[DynamicDependency(DynammicallyAccessedMemberTypes.All, "*", "Microsoft.Maui.Controls")].
- This would only address the issue for our built-in controls. It wouldn't solve the problem for 3rd party extenders who wanted to allow their control assemblies to be trimmed.
-
We could add a Feature Switch to the LoadFromXaml behavior. Let's call it Microsoft.Maui.LoadFromXaml.IsSupported. When LoadFromXaml.IsSupported=false, calling LoadFromXaml will throw an exception instructing the developer to set LoadFromXaml.IsSupported=true. By default in a trimmed, Release built version of an app, LoadFromXaml.IsSupported will be false. For all other times, it will be true. The benefit that this approach has is when LoadFromXaml.IsSupported=true we can use an ILLink.Descriptor.xml file that will tell the trimmer "when LoadFromXaml.IsSupported=true, then preserve this whole assembly".
- 3rd party extenders could also follow this approach. That way their control assemblies can be marked as "Trimmable", but when LoadFromXaml is enabled, their whole assembly can be preserved as well.
Description
Control -> Handler Mapping
maui/src/Controls/src/Xaml/Hosting/AppHostBuilderExtensions.cs
Lines 49 to 59 in 1ba45f9
We have this massive list of direct references to XAML controls to handlers.
We could use something like this: #7258
Linking
Initially from #5028 (comment)
The issue is that Xaml is a form of code that the trimmer can't see. It can reference Types, and since the trimmer can't see which Types it uses, the trimmer doesn't know it needs to keep those Types.
I can think of the following options to resolve this:
Don't mark Microsoft.Maui.Controls as "Trimmable".
Microsoft.Maui.dll. This would be at least a step in the right direction.Take this change as is. And if someone is using LoadFromXaml in their app, instruct them to add
<TrimmerRootAssembly Include="Microsoft.Maui.Controls" />to their .csproj. This will instruct the trimmer to preserve the entire assembly. That way people who don't use LoadFromXaml can save the size off their apps. And people who do use it can make it work.We could add a
[DynamicDependency]on the LoadFromXaml API. This way, if this method is used in the app, the Types can be preserved. However, there are the following drawbacks with this approach:[DynamicDependency(DynammicallyAccessedMemberTypes.All, "*", "Microsoft.Maui.Controls")].We could add a Feature Switch to the
LoadFromXamlbehavior. Let's call itMicrosoft.Maui.LoadFromXaml.IsSupported. WhenLoadFromXaml.IsSupported=false, calling LoadFromXaml will throw an exception instructing the developer to setLoadFromXaml.IsSupported=true. By default in a trimmed, Release built version of an app,LoadFromXaml.IsSupportedwill befalse. For all other times, it will betrue. The benefit that this approach has is whenLoadFromXaml.IsSupported=truewe can use an ILLink.Descriptor.xml file that will tell the trimmer "when LoadFromXaml.IsSupported=true, then preserve this whole assembly".