From 6d04b5d6f1bd64cd2a5a9c6ea9d5d59ca291a245 Mon Sep 17 00:00:00 2001 From: David Britch Date: Tue, 8 Oct 2024 15:06:39 +0100 Subject: [PATCH] Loading XAML at runtime isn't trim safe (#2552) * Loading XAML at runtime isn't trim safe. * Edit. * Version new addition. * Edit. * Edit. --- docs/whats-new/dotnet-9.md | 6 +++++- docs/xaml/runtime-load.md | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/whats-new/dotnet-9.md b/docs/whats-new/dotnet-9.md index 3936644b..ef72115a 100644 --- a/docs/whats-new/dotnet-9.md +++ b/docs/whats-new/dotnet-9.md @@ -359,6 +359,10 @@ video.DisconnectHandlers(); When disconnecting, the `DisconnectHandlers` method will propagate down the control tree until it completes or arrives at a control that has set a manual policy. +## Load XAML at runtime + +Loading XAML at runtime isn't trim safe and shouldn't be used with full trimming or NativeAOT. It can be made trim safe by annotating all types that could be loaded at runtime with the [`DynamicallyAccessedMembers`](xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute) attribute or the [`DynamicDependency`](xref:System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute) attribute. However, this is very error prone and isn't recommended. + ## Multi-window support .NET MAUI 9 adds the ability to bring a specific window to the front on Mac Catalyst and Windows with the `Application.Current.ActivateWindow` method: @@ -439,7 +443,7 @@ Several areas of .NET MAUI come with trimmer directives, known as feature switch To consume a feature switch you should put the corresponding MSBuild property into your app's project file (*.csproj), which causes the related code to be trimmed from the .NET MAUI assemblies. Disabling features an app doesn't require can help reduce the app size when combined with the `Full` trimming mode. -## XAML +## XAML markup extensions All classes that implement , , , and need to be annotated with either the or . This is required due to a XAML compiler optimization introduced in .NET MAUI 9 that enables the generation of more efficient code, which helps reduce the app size and improve runtime performance. diff --git a/docs/xaml/runtime-load.md b/docs/xaml/runtime-load.md index 4e8c1d67..44801001 100644 --- a/docs/xaml/runtime-load.md +++ b/docs/xaml/runtime-load.md @@ -1,7 +1,7 @@ --- title: "Load XAML at runtime" description: "In .NET MAUI, XAML can be loaded and parsed at runtime with the LoadFromXaml extension methods." -ms.date: 01/26/2022 +ms.date: 10/08/2024 --- # Load XAML at runtime @@ -18,7 +18,7 @@ public partial class MainPage : ContentPage } ``` -When Visual Studio builds a project containing a XAML file, a source generator generates new C# source that contains the definition of the `InitializeComponent` method and adds it to the compilation object. The following example shows the generated `InitializeComponent` method for the `MainPage` class: +When a project containing a XAML file is built, a source generator generates new C# source that contains the definition of the `InitializeComponent` method and adds it to the compilation object. The following example shows the generated `InitializeComponent` method for the `MainPage` class: ```csharp private void InitializeComponent() @@ -34,9 +34,20 @@ The `InitializeComponent` method calls the extension methods that can be used to load and parse XAML at runtime. The methods are `public`, and therefore can be called from .NET MAUI applications to load, and parse XAML at runtime. This enables scenarios such as an app downloading XAML from a web service, creating the required view from the XAML, and displaying it in the app. +::: moniker range="=net-maui-8.0" + > [!WARNING] > Loading XAML at runtime has a significant performance cost, and generally should be avoided. +::: moniker-end + +::: moniker range=">=net-maui-9.0" + +> [!WARNING] +> Loading XAML at runtime isn't trim safe and shouldn't be used with full trimming or NativeAOT. It can be made trim safe by annotating all types that could be loaded at runtime with the [`DynamicallyAccessedMembers`](xref:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute) attribute or the [`DynamicDependency`](xref:System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute) attribute. However, this is very error prone and isn't recommended. In addition, loading XAML at runtime has a significant performance cost. + +::: moniker-end + The following code example shows a simple usage: ```csharp