Skip to content

Commit

Permalink
docs: HowTo IRouteNotifier
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Sep 9, 2024
1 parent e5fcb78 commit 721e642
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
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

0 comments on commit 721e642

Please sign in to comment.