Skip to content

Commit 0686c75

Browse files
authored
Single use query parameters for GoToAsync (#1815)
* Single use query parameters. * Edit. * Edits.
1 parent e6c92ee commit 0686c75

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

docs/fundamentals/shell/navigation.md

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: ".NET MAUI Shell navigation"
33
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."
4-
ms.date: 04/07/2022
4+
ms.date: 10/23/2023
55
---
66

77
# .NET MAUI Shell navigation
@@ -352,7 +352,7 @@ async void OnCollectionViewSelectionChanged(object sender, SelectionChangedEvent
352352

353353
This example retrieves the currently selected elephant in the <xref:Microsoft.Maui.Controls.CollectionView>, and navigates to the `elephantdetails` route, passing `elephantName` as a query parameter.
354354

355-
Object-based navigation data can be passed with a `GoToAsync` overload that specifies an `IDictionary<string, object>` argument:
355+
Multiple use object-based navigation data can be passed with a `GoToAsync` overload that specifies an `IDictionary<string, object>` argument:
356356

357357
```csharp
358358
async void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
@@ -368,15 +368,53 @@ async void OnCollectionViewSelectionChanged(object sender, SelectionChangedEvent
368368

369369
This example retrieves the currently selected bear in the <xref:Microsoft.Maui.Controls.CollectionView>, as an `Animal`. The `Animal` object is added to a `Dictionary` with the key `Bear`. Then, navigation to the `beardetails` route is performed, with the `Dictionary` being passed as a navigation parameter.
370370

371+
Any data that's passed as an `IDictionary<string, object>` argument is retained in memory for the lifetime of the page, and isn't released until the page is removed from the navigation stack. This can be problematic, as shown in the following scenario:
372+
373+
1. `Page1` navigates to `Page2` using the `GoToAsync` method, passing in an object called `MyData`. `Page2` then receives `MyData` as a query parameter.
374+
1. `Page2` navigates to `Page3` using the `GoToAsync` method, without passing any data.
375+
1. `Page3` navigates backwards with the `GoToAsync` method. `Page2` then receives `MyData` again as a query parameter.
376+
377+
While this is desirable in many scenarios, if it isn't desired you should clear the `IDictionary<string, object>` argument with the `Clear` method after it's first been received by a page.
378+
379+
::: moniker range=">=net-maui-8.0"
380+
381+
Alternatively, you can use the `GoToAsync` overload that enables you to pass single use navigation data as a `ShellNavigationQueryParameters` object. A `ShellNavigationQueryParameters` object is intended for navigation data that's cleared after navigation has occurred. The following example shows navigating while passing data as a `ShellNavigationQueryParameters` object:
382+
383+
```csharp
384+
async void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
385+
{
386+
Animal animal = e.CurrentSelection.FirstOrDefault() as Animal;
387+
var navigationParameter = new ShellNavigationQueryParameters
388+
{
389+
{ "Bear", animal }
390+
};
391+
await Shell.Current.GoToAsync($"beardetails", navigationParameter);
392+
}
393+
```
394+
395+
This example retrieves the currently selected bear in the <xref:Microsoft.Maui.Controls.CollectionView>, as an `Animal` that's added to the `ShellNavigationQueryParameters` object. Then, navigation to the `beardetails` route is performed, with the `ShellNavigationQueryParameters` object being passed as a navigation parameter. After navigation has occurred the data in the `ShellNavigationQueryParameters` object is cleared.
396+
397+
::: moniker-end
398+
371399
There are two approaches to receiving navigation data:
372400

373401
1. The class that represents the page being navigated to, or the class for the page's `BindingContext`, can be decorated with a `QueryPropertyAttribute` for each query parameter. For more information, see [Process navigation data using query property attributes](#process-navigation-data-using-query-property-attributes).
374402
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).
375403

376404
### Process navigation data using query property attributes
377405

406+
::: moniker range="=net-maui-7.0"
407+
378408
Navigation data can be received by decorating the receiving class with a `QueryPropertyAttribute` for each string-based query parameter and object-based navigation parameter:
379409

410+
::: moniker-end
411+
412+
::: moniker range=">=net-maui-8.0"
413+
414+
Navigation data can be received by decorating the receiving class with a `QueryPropertyAttribute` for each string-based query parameter, object-based navigation parameter, or `ShellNavigationQueryParameters` object:
415+
416+
::: moniker-end
417+
380418
```csharp
381419
[QueryProperty(nameof(Bear), "Bear")]
382420
public partial class BearDetailPage : ContentPage

docs/whats-new/dotnet-8.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ For information about what's new in .NET 8, see [What's new in .NET 8](/dotnet/c
3939
- The <xref:Microsoft.Maui.ApplicationModel.Permissions> class gains the `Bluetooth` permission, which is an Android 12 permission for looking for Bluetooth devices, making the current device discoverable to other Bluetooth devices, and communicating with already-paired Bluetooth devices. For more information, see [Permissions](~/platform-integration/appmodel/permissions.md).
4040
- The <xref:Microsoft.Maui.ApplicationModel.Permissions> class gains the `NearbyWifiDevices` permission, which is an Android 13 permission for accessing nearby WiFi devices. For more information, see [Permissions](~/platform-integration/appmodel/permissions.md).
4141
- Several system fonts can be easily consumed in Android apps. For more information, see [Consume fonts](~/user-interface/fonts.md#consume-fonts).
42+
- Shell navigation gains a `GoToAsync` overload that enables you to pass single use navigation data, that's cleared after navigation has occurred, as a `ShellNavigationQueryParameters` object. For more information, see [Pass data](~/fundamentals/shell/navigation.md#pass-data).
4243

4344
The following types or members have been deprecated:
4445

0 commit comments

Comments
 (0)