Skip to content

Navigation And Stack

Omid Mafakher edited this page Mar 27, 2023 · 3 revisions

Navigator and Stack system

Navigator

The Navigator is a key component of the AvaloniaInside.Shell library that is responsible for handling navigation within an application. It provides a simple API for navigating between different pages or views in the application, and it manages the state of the navigation stack.

The Navigator works by using the registered routes defined in the NavigationRegistrar to match incoming requests to a specific NavigationNode. The Navigator then creates a NavigationChain based on the matched NavigationNode and adds it to the NavigationStack.

Registrar

The NavigationRegistrar is responsible for registering the available routes and their corresponding NavigationNodes. The NavigationRegistrar is also responsible for creating and managing the hierarchy of NavigationNodes that represent different parts of the application.

The NavigationRegistrar provides a simple API for registering routes and their corresponding NavigationNodes, and it manages the state of the registered NavigationNodes.

Navigation Node System

The Navigation Node System is the core of the AvaloniaInside.Shell library. It is responsible for defining and managing the hierarchy of NavigationNodes that represent different parts of the application.

NavigationNodes are objects that represent different parts of the application, such as a page or a view. Each NavigationNode has a unique identifier, a NavigationNodeType that defines its type, and a set of properties that define its behavior and appearance.

NavigationNodes can be organized into a hierarchy that reflects the structure of the application. For example, a NavigationNode representing a home page might have child nodes representing different sections of the site.

Navigation Stack

The NavigationStack is a data structure that is used by the Navigator to keep track of the user's navigation history. It is implemented as a stack, with each NavigationChain representing a single navigation step.

NavigationChains are created by the Navigator based on the NavigationNodes that match incoming requests. Each NavigationChain contains information about the NavigationNode, the type of navigation, and the URI that was used to make the request.

The NavigationStack provides a simple API for adding and removing NavigationChains, and it allows the Navigator to easily navigate back and forth through the user's history.

Host

In the context of the Shell library, a host is a container for one or more pages. Each host has a unique identifier and a collection of NavigationNodes representing the pages it contains. When a page is navigated to, it is displayed within its containing host.

A host can be a standalone container, or it can be nested within another host, creating a hierarchical structure of hosts and pages. For example, a TabControl could be a host that contains multiple pages, each represented by a tab. Each tab would be a nested host within the TabControl host.

The Stack maintains a hierarchical NavigationChain to keep track of the currently displayed page and its containing host(s).

Route

A route is a mapping between a URL or URI and a NavigationNode. It is used to determine which page should be displayed when a specific URL is requested.

Routes can be defined with a specific URL pattern and a NavigationNode representing the page to be displayed. When a URL is requested, the Navigator uses the defined routes to determine which NavigationNode to display.

Naviagte strategies

NaturalNavigateStrategy

Implements the INavigateStrategy interface. Uses a INavigationRegistrar to resolve the route to the target page and create the new URL based on the current URL and the relative path provided. The NavigateAsync method creates a new URL by combining the current URL and the relative path provided as a parameter. It then uses the INavigationRegistrar to get the last default node of the resolved hosts and create a new URI for it. If the relative path has a query, it adds it to the new URI's query. If both the relative path and the new URI have a hash tag, it combines them. Finally, it returns the new URI. The BackAsync method returns the URI for the parent directory of the current URL.

RelativeNavigateStrategy

Extends the NaturalNavigateStrategy class. The BackAsync method returns the URI of the first non-host navigation chain in the back chain.

Navigator

Registrar

The Registrar property returns the navigation registrar associated with the navigator. The registrar is responsible for registering all the routes and hosts used by the navigator.

INavigationRegistrar Registrar { get; }

HasItemInStack

The HasItemInStack method returns a boolean indicating whether there is any item in the navigation stack.

bool HasItemInStack();

NavigateAsync

The NavigateAsync method navigates to the specified URI. If an argument is provided, it will be passed to the view model associated with the target page. If a navigate type is provided, it will determine how the page is presented.

Task NavigateAsync(string path, CancellationToken cancellationToken = default);
Task NavigateAsync(string path, object? argument, CancellationToken cancellationToken = default);
Task NavigateAsync(string path, NavigateType? navigateType, CancellationToken cancellationToken = default);
Task NavigateAsync(string path, NavigateType? navigateType, object? argument, CancellationToken cancellationToken = default);

NavigateAndWaitAsync

Navigates to the specified path and waits until the next page is closed before returning the result.

Task<NavigateResult> NavigateAndWaitAsync(string path, CancellationToken cancellationToken = default);
Task<NavigateResult> NavigateAndWaitAsync(string path, object? argument, CancellationToken cancellationToken = default);
Task<NavigateResult> NavigateAndWaitAsync(string path, NavigateType navigateType, CancellationToken cancellationToken = default);
Task<NavigateResult> NavigateAndWaitAsync(string path, object? argument, NavigateType navigateType, CancellationToken cancellationToken = default);

BackAsync

The BackAsync method navigates back to the previous page in the navigation stack. If an argument is provided, it will be passed to the view model associated with the previous page.

The BackAsync method navigates back to the previous page in the navigation stack. If an argument is provided, it will be passed to the view model associated with the previous page.