QueryPropertyAttribute isn't trim safe. (#2554)

* QueryPropertyAttribute isn't trim safe.

* Edits.

* Version added content.
This commit is contained in:
David Britch 2024-10-08 15:16:13 +01:00 коммит произвёл GitHub
Родитель aad6128966
Коммит 2e68899150
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 26 добавлений и 10 удалений

Просмотреть файл

@ -1,7 +1,7 @@
---
title: ".NET MAUI Shell navigation"
description: "Learn how .NET MAUI Shell apps can utilize a URI-based navigation experience that permits navigation to any page in the app, without having to follow a set navigation hierarchy."
ms.date: 08/30/2024
ms.date: 10/08/2024
---
# .NET MAUI Shell navigation
@ -401,7 +401,7 @@ This example retrieves the currently selected bear in the <xref:Microsoft.Maui.C
There are two approaches to receiving navigation data:
1. The class that represents the page being navigated to, or the class for the page's `BindingContext`, can be decorated with a <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> for each query parameter. For more information, see [Process navigation data using query property attributes](#process-navigation-data-using-query-property-attributes).
1. The class that represents the page being navigated to, or the class for the page's `BindingContext`, can implement the `IQueryAttributable` interface. For more information, see [Process navigation data using a single method](#process-navigation-data-using-a-single-method).
1. The class that represents the page being navigated to, or the class for the page's `BindingContext`, can implement the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface. For more information, see [Process navigation data using a single method](#process-navigation-data-using-a-single-method).
### Process navigation data using query property attributes
@ -432,14 +432,21 @@ public partial class BearDetailPage : ContentPage
In this example the first argument for the <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> specifies the name of the property that will receive the data, with the second argument specifying the parameter id. Therefore, the <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> in the above example specifies that the `Bear` property will receive the data passed in the `Bear` navigation parameter in the <xref:Microsoft.Maui.Controls.Shell.GoToAsync%2A> method call.
> [!NOTE]
> [!IMPORTANT]
> String-based query parameter values that are received via the <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> are automatically URL decoded.
::: moniker range=">=net-maui-9.0"
> [!WARNING]
> Receiving navigation data using the <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> isn't trim safe and shouldn't be used with full trimming or NativeAOT. Instead, you should implement the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface on types that need to accept query parameters. For more information, see [Process navigation data using a single method](#process-navigation-data-using-a-single-method).
::: moniker-end
### Process navigation data using a single method
Navigation data can be received by implementing the `IQueryAttributable` interface on the receiving class. The `IQueryAttributable` interface specifies that the implementing class must implement the `ApplyQueryAttributes` method. This method has a `query` argument, of type `IDictionary<string, object>`, that contains any data passed during navigation. Each key in the dictionary is a query parameter id, with its value corresponding to the object that represents the data. The advantage of using this approach is that navigation data can be processed using a single method, which can be useful when you have multiple items of navigation data that require processing as a whole.
Navigation data can be received by implementing the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface on the receiving class. The <xref:Microsoft.Maui.Controls.IQueryAttributable> interface specifies that the implementing class must implement the `ApplyQueryAttributes` method. This method has a `query` argument, of type `IDictionary<string, object>`, that contains any data passed during navigation. Each key in the dictionary is a query parameter id, with its value corresponding to the object that represents the data. The advantage of using this approach is that navigation data can be processed using a single method, which can be useful when you have multiple items of navigation data that require processing as a whole.
The following example shows a view model class that implements the `IQueryAttributable` interface:
The following example shows a view model class that implements the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface:
```csharp
public class MonkeyDetailViewModel : IQueryAttributable, INotifyPropertyChanged
@ -458,7 +465,7 @@ public class MonkeyDetailViewModel : IQueryAttributable, INotifyPropertyChanged
In this example, the `ApplyQueryAttributes` method retrieves the object that corresponds to the `Monkey` key in the `query` dictionary, which was passed as an argument to the <xref:Microsoft.Maui.Controls.Shell.GoToAsync%2A> method call.
> [!IMPORTANT]
> String-based query parameter values that are received via the `IQueryAttributable` interface aren't automatically URL decoded.
> String-based query parameter values that are received via the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface aren't automatically URL decoded.
#### Pass and process multiple items of data
@ -503,7 +510,14 @@ public partial class ElephantDetailPage : ContentPage
In this example, the class is decorated with a <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> for each query parameter. The first <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> specifies that the `Name` property will receive the data passed in the `name` query parameter, while the second <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> specifies that the `Location` property will receive the data passed in the `location` query parameter. In both cases, the query parameter values are specified in the URI in the <xref:Microsoft.Maui.Controls.Shell.GoToAsync%2A> method call.
Alternatively, navigation data can be processed by a single method by implementing the `IQueryAttributable` interface on the class that represents the page being navigated to, or the class for the page's `BindingContext`:
::: moniker range=">=net-maui-9.0"
> [!WARNING]
> Receiving navigation data using the <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> isn't trim safe and shouldn't be used with full trimming or NativeAOT. Instead, you should implement the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface on types that need to accept query parameters.
::: moniker-end
Alternatively, navigation data can be processed by a single method by implementing the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface on the class that represents the page being navigated to, or the class for the page's `BindingContext`:
```csharp
public class ElephantDetailViewModel : IQueryAttributable, INotifyPropertyChanged

Просмотреть файл

@ -12,7 +12,7 @@ Several areas of .NET MAUI come with trimmer directives, known as feature switch
| ---------------- | ----------------- | ----------- |
| `MauiEnableVisualAssemblyScanning` | `Microsoft.Maui.RuntimeFeature.IsIVisualAssemblyScanningEnabled` | When set to `true`, .NET MAUI will scan assemblies for types implementing `IVisual` and for `[assembly:Visual(...)]` attributes, and will register these types. By default, this build property is set to `false`. |
| `MauiShellSearchResultsRendererDisplayMemberNameSupported` | `Microsoft.Maui.RuntimeFeature.IsShellSearchResultsRendererDisplayMemberNameSupported` | When set to `false`, the value of `SearchHandler.DisplayMemberName` will be ignored. Instead, you should provide an `ItemTemplate` to define the appearance of `SearchHandler` results. By default, this build property is set to `true`.|
| `MauiQueryPropertyAttributeSupport` | `Microsoft.Maui.RuntimeFeature.IsQueryPropertyAttributeSupported` | When set to `false`, `[QueryProperty(...)]` attributes won't be used to set property values when navigating. Instead, you should implement the `IQueryAttributable` interface to accept query parameters. By default, this build property is set to `true`. |
| `MauiQueryPropertyAttributeSupport` | `Microsoft.Maui.RuntimeFeature.IsQueryPropertyAttributeSupported` | When set to `false`, `[QueryProperty(...)]` attributes won't be used to set property values when navigating. Instead, you should implement the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface to accept query parameters. By default, this build property is set to `true`. |
| `MauiImplicitCastOperatorsUsageViaReflectionSupport` | `Microsoft.Maui.RuntimeFeature.IsImplicitCastOperatorsUsageViaReflectionSupported` | When set to `false`, .NET MAUI won't look for implicit cast operators when converting values from one type to another. This can affect bindings between properties with different types, and setting a property value of a bindable object with a value of a different type. Instead, you should define a `TypeConverter` for your type and attach it to the type using the `[TypeConverter(typeof(MyTypeConverter))]` attribute. By default, this build property is set to `true`.|
| `_MauiBindingInterceptorsSupport` | `Microsoft.Maui.RuntimeFeature.AreBindingInterceptorsSupported` | When set to `false`, .NET MAUI won't intercept any calls to the `SetBinding` methods and won't try to compile them. By default, this build property is set to `true`. |

Просмотреть файл

@ -71,7 +71,7 @@ Create the **Note viewmodel**:
These methods are invoked by associated commands. They perform the related actions on the model and make the app navigate to the previous page. A query string parameter is added to the `..` navigation path, indicating which action was taken and the note's unique identifier.
01. Next, add the `ApplyQueryAttributes` method to the class, which satisfies the requirements of the `IQueryAttributable` interface:
01. Next, add the `ApplyQueryAttributes` method to the class, which satisfies the requirements of the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface:
:::code language="csharp" source="../snippets/viewmodel-shared/ViewModels/NoteViewModel.cs" id="iquery":::

Просмотреть файл

@ -431,6 +431,8 @@ In .NET MAUI 9, a stand-alone XAML <xref:Microsoft.Maui.Controls.ResourceDiction
## Shell apps
Receiving navigation data using the <xref:Microsoft.Maui.Controls.QueryPropertyAttribute> isn't trim safe and shouldn't be used with full trimming or NativeAOT. Instead, you should implement the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface on types that need to accept query parameters. For more information, see [Process navigation data using a single method](~/fundamentals/shell/navigation.md#process-navigation-data-using-a-single-method).
`SearchHandler.DisplayMemberName` isn't trim safe and shouldn't be used with full trimming or NativeAOT. Instead, you should provide an `ItemTemplate` to define the appearance of `SearchHandler` results. For more information, see [Define search results item appearance](~/fundamentals/shell/search.md#define-search-results-item-appearance).
## Trimming feature switches
@ -441,7 +443,7 @@ Several areas of .NET MAUI come with trimmer directives, known as feature switch
| ---------------- | ----------- |
| `MauiEnableVisualAssemblyScanning` | When set to `true`, .NET MAUI will scan assemblies for types implementing `IVisual` and for `[assembly:Visual(...)]` attributes, and will register these types. By default, this build property is set to `false`. |
| `MauiShellSearchResultsRendererDisplayMemberNameSupported` | When set to `false`, the value of `SearchHandler.DisplayMemberName` will be ignored. Instead, you should provide an `ItemTemplate` to define the appearance of `SearchHandler` results. By default, this build property is set to `true`.|
| `MauiQueryPropertyAttributeSupport` | When set to `false`, `[QueryProperty(...)]` attributes won't be used to set property values when navigating. Instead, you should implement the `IQueryAttributable` interface to accept query parameters. By default, this build property is set to `true`. |
| `MauiQueryPropertyAttributeSupport` | When set to `false`, `[QueryProperty(...)]` attributes won't be used to set property values when navigating. Instead, you should implement the <xref:Microsoft.Maui.Controls.IQueryAttributable> interface to accept query parameters. By default, this build property is set to `true`. |
| `MauiImplicitCastOperatorsUsageViaReflectionSupport` | When set to `false`, .NET MAUI won't look for implicit cast operators when converting values from one type to another. This can affect bindings between properties with different types, and setting a property value of a bindable object with a value of a different type. Instead, you should define a `TypeConverter` for your type and attach it to the type using the `[TypeConverter(typeof(MyTypeConverter))]` attribute. By default, this build property is set to `true`.|
| `_MauiBindingInterceptorsSupport` | When set to `false`, .NET MAUI won't intercept any calls to the `SetBinding` methods and won't try to compile them. By default, this build property is set to `true`. |