Skip to content

RoutingState class

Attributes: [DataContract] [DebuggerDisplay("NavigationStack Count = {NavigationStack.Count}")]

Defined in

Namespace: ReactiveUI.Reactive Assembly: ReactiveUI.Reactive.dll Full name: ReactiveUI.Reactive.RoutingState Modifiers: public

Summary

View source

RoutingState manages the ViewModel Stack and allows ViewModels to navigate to other ViewModels.

Applies to

net10.0, net10.0-ios26.0, net10.0-android36.0, net10.0-maccatalyst26.0, net10.0-macos26.0, net10.0-tvos26.0, net9.0, net8.0, net462, net481

Class hierarchy
classDiagram
class RoutingState
class ReactiveObject
ReactiveObject <|-- RoutingState

Inherits from: ReactiveObject

Remarks

Use RoutingState from an IScreen implementation to coordinate navigation in multi-page applications. The stack works in a last-in-first-out fashion, enabling forward navigation via Navigate and back navigation via NavigateBack.

Every navigation notification is a plain observable of values: CurrentViewModel reports the view model on top of the stack, NavigationStackChanged reports the whole stack after each change, and CanNavigateBack reports whether there is a view model to go back to.

Examples

<![CDATA[
 public class ShellViewModel : ReactiveObject, IScreen
 {
 public RoutingState Router { get; } = new();
 
 public ShellViewModel()
 {
 Router.Navigate.Execute(new HomeViewModel(this)).Subscribe();
 }
 
 public void ShowDetails() =>
 Router.Navigate.Execute(new DetailsViewModel(this)).Subscribe();
 
 public void GoBack() =>
 Router.NavigateBack.Execute(Unit.Default).Subscribe();
 }
 
 public partial class ShellView : ReactiveUserControl<ShellViewModel>
 {
 public ShellView()
 {
 this.WhenActivated(disposables =>
 ViewModel!.Router.CurrentViewModel
 .Subscribe(viewModel => contentHost.NavigateTo(viewModel))
 .DisposeWith(disposables));
 }
 }
 ]]>

Constructors

NameSummary
.ctorInitializes a new instance of the [RoutingState](# class using the default main thread scheduler.

Properties

NameSummary
NavigationStackGets or sets the current navigation stack, with the last element representing the active view model.
NavigateBackGets a command which will navigate back to the previous element in the stack and emits the new current view model. The command can only execute when at least two view models exist in the...
NavigateGets a command that adds a new element to the navigation stack. The command argument must implement [IRoutableViewModel](# and the command emits the same...
NavigateAndResetGets a command that replaces the entire navigation stack with the supplied view model, effectively resetting navigation history.
CurrentViewModelGets an observable of the view model on top of the navigation stack. It emits the current view model when you subscribe, then again after every change to the stack. It emits null while the stack...
NavigationStackChangedGets an observable of the navigation stack. After every change to [NavigationStack](# it emits a read-only snapshot of the whole stack, oldest view...
CanNavigateBackGets an observable that reports whether there is a view model to go back to, which is true while the stack holds at least two view models. It emits the current answer when you subscribe, then again...
Inherited members

Extension members