Navigation Service
Overview
The NavigationService is a central infrastructural component of Asv.Avalonia responsible for page-based navigation and focus management. It implements the INavigationService and is designed to work closely with the IRoutable View Models.
When to Use
In most standard scenarios, you do not need to interact with Navigation Service directly. The framework handles common navigation tasks via standard commands (like OpenPageCommandBase). However, direct usage of the Navigation Service is required in the following scenarios:
Complex Navigation Logic: When the destination page depends on runtime conditions (e.g., "If login succeeds → Go Home, otherwise → Show Error").
Navigation from Non-UI Contexts: Triggering navigation from background services, timers, or external events (e.g., deep linking).
Manual History Management: When you need to programmatically trigger Backward or Forward actions outside of standard UI bindings.
Focus Management: When you need to force focus on a specific
IRoutableelement (e.g., focusing a specific widget or input field after a validation error).
Key Features
Path-Based Navigation: Navigate to any
ViewModelhierarchy usingNavigationPath(similar to URL routing).History Management: Automatically maintains Forward/Backward stacks using
ObservableStack.Automatic Focus Tracking:
The service listens to global UI events (
GotFocus,PointerPressed) on the window.When a user clicks a control inside a view, the service automatically finds the corresponding
IRoutableViewModel in the data context and updates theSelectedControlproperty.This ensures the "Active Page" state is always synced with what the user is actually interacting with.
How it works
The Shell Relationship
The Navigation Service doesn't "render" views itself. Instead, it delegates the actual navigation logic to the IShell. When you call GoTo, the service:
Validates the path.
Delegates the routing to the Shell via
_host.Shell.NavigateByPath().Updates the focus to the resulting ViewModel.
Focus & Selection Logic
This is the most complex part of the service.
Manual Navigation: When calling
GoTo, the service explicitly sets focus to the target ViewModel (if it implementsISupportFocus).User Interaction: If the user clicks on a textbox deep inside a page, the service catches this event, walks up the visual/logical tree to find the nearest
IRoutableparent, and sets it as theSelectedControl.
Usage Examples
Using Navigation Commands
Many built-in commands use the Navigation Service to open pages:
Basic Navigation
To navigate to a specific page, you can use the GoTo method:
API
INavigationService
Represents a navigation service for handling application navigation, navigation history, and focus management.
Properties
Property | Type | Description |
|---|---|---|
|
| Gets the observable collection representing the backward navigation history. |
|
| Gets the сommand that triggers backward navigation. |
|
| Gets the observable collection representing the forward navigation history. |
|
| Gets the сommand that triggers forward navigation. |
|
| Gets the сurrently selected (focused) routable control. |
|
| Gets the navigation path of the currently selected control. |
|
| Gets the сommand that triggers navigation to the home page. |
Methods
Method | Return Type | Description |
|---|---|---|
|
| Navigates to the previous item in the backward navigation stack. |
|
| Navigates to the next item in the forward navigation stack. |
|
| Navigates to the specified navigation path and returns the resulting routable control. |
|
| Navigates to the home page. |
|
| Forces focus change to the specified routable control. |
INavigationService.GoTo
Parameter | Type | Description |
|---|---|---|
|
| Path to navigate to. |
INavigationService.ForceFocus
Parameter | Type | Description |
|---|---|---|
|
| Routable control to be set as currently focused. |