Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: HowTo IRouteNotifier #2388

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions doc/Learn/Navigation/Advanced/HowTo-IRouteNotifier.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
uid: Uno.Extensions.Navigation.Advanced.IRouteNotifier
---
# How-To: Use `IRouteNotifier` to Handle Route Changes

The `IRouteNotifier` interface allows you to track and respond to route changes through the `RouteChanged` event. This guide will show you how to use `IRouteNotifier` to handle route changes and improve your app's navigation.

## Step-by-steps

[!include[create-application](../../includes/create-application.md)]

### How to Use `IRouteNotifier` to Monitor Route Changes

The `IRouteNotifier` provides an effective way to monitor and respond to route changes within your application. To begin using `IRouteNotifier`, ensure your class has access to an instance of an `IRouteNotifier` implementation. Add a parameter of type `IRouteNotifier` to the constructor of your class where you want to monitor route changes.

```csharp
public class MyClass
{
private readonly IRouteNotifier _notifier;

public MyClass(IRouteNotifier notifier)
{
_notifier = notifier;
_notifier.RouteChanged += RouteChanged;
}

private async void RouteChanged(object? sender, RouteChangedEventArgs e)
{
// Implement your logic to handle route change here
}
}
```

It's possible to access the `IRouteNotifier` service directly from `App.xaml.cs` or anywhere you have access to the `IHost` to retrieve services. This allows you to subscribe to route changes and respond accordingly in your application.

For example, in `App.xaml.cs`:

```csharp
...
Host = await builder.NavigateAsync<Shell>();

var notifier = Host.Services.GetService<IRouteNotifier>();
notifier.RouteChanged += (s, e) =>
{
Debug.WriteLine($"Navigated to {e.Region?.Name}");
};
```

### Access `INavigator` through `IRouteNotifier`

It's possible to access an `INavigator` through the `RouteChanged` event provided by the `IRouteNotifier`. This can be particularly useful when you need to handle navigation within dynamic scenarios, such as managing modals, dialogs, or conditional navigation flows.

```csharp
private async void RouteChanged(object? sender, RouteChangedEventArgs e)
{
var navigator = e.Navigator;
// Logic to use `INavigator` here
}
```
2 changes: 2 additions & 0 deletions doc/Learn/Navigation/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
href: xref:Uno.Extensions.Navigation.Advanced.Panel
- name: "How-To: Navigate using a ContentControl"
href: xref:Uno.Extensions.Navigation.Advanced.ContentControl
- name: "How-To: Use IRouteNotifier to Handle Route Changes"
href: xref:Uno.Extensions.Navigation.Advanced.IRouteNotifier
Loading