public class NavigationHost : ContentControl Presents the current entry of a navigation region.
Remarks
Each entry gets a new ContentPresenter and a new view, also when two entries have the same type or the same borrowed content, so no view state leaks from one entry to the next. By default a retained entry gets a new view when it becomes current again: keep state that must survive in the ViewModel, or set NavigationHost.RetainViews to keep the view of each retained entry and show it again. NavigationRequestOptions.RetainView decides it for one entry instead.
The view comes from NavigationHost.ViewLocator, then from an INavigationViewLocator registered in the navigator's services, then from the implicit DataTemplate keyed by the content type or a base type. The host follows the region while it is loaded, and never changes entries when it is unloaded. NavigationCommands.BrowseBack goes back when the region can go back and isn't transitioning, nor busy when it admits requests with NavigationAdmission.RejectWhileBusy.
The host sets its own ContentControl.Content: don't set Content, ContentTemplate or ContentTemplateSelector on it. Present entries with implicit DataTemplates or a locator.
With NavigationHost.GuardWindowClose, closing the host's window clears the region first, so the departure guards that navigation asks also decide whether the window closes. The window keeps guarding the region while the host is unloaded, as on an unselected tab.
Constructors
NavigationHost
public NavigationHost() Creates a host.
Fields
EmptyContentProperty
public static readonly DependencyProperty EmptyContentProperty Identifies the NavigationHost.EmptyContent dependency property.
GuardWindowCloseProperty
public static readonly DependencyProperty GuardWindowCloseProperty Identifies the NavigationHost.GuardWindowClose dependency property.
HandlesBrowseBackProperty
public static readonly DependencyProperty HandlesBrowseBackProperty Identifies the NavigationHost.HandlesBrowseBack dependency property.
IsBusyProperty
public static readonly DependencyProperty IsBusyProperty Identifies the NavigationHost.IsBusy dependency property.
IsTransitioningProperty
public static readonly DependencyProperty IsTransitioningProperty Identifies the NavigationHost.IsTransitioning dependency property.
RegionProperty
public static readonly DependencyProperty RegionProperty Identifies the NavigationHost.Region dependency property.
RetainViewsProperty
public static readonly DependencyProperty RetainViewsProperty Identifies the NavigationHost.RetainViews dependency property, which is inherited.
ViewLocatorProperty
public static readonly DependencyProperty ViewLocatorProperty Identifies the NavigationHost.ViewLocator dependency property.
Properties
EmptyContent
public object? EmptyContent { get; set; } Gets or sets the content shown while the region is empty or no region is set.
GuardWindowClose
public bool GuardWindowClose { get; set; } Gets or sets whether closing the host's window first clears the region, asking the departure guards of every entry and of the child regions they own. The default is false.
Remarks
When the host loads with this on, its window starts guarding the region. The window keeps guarding it after the host unloads or is discarded, as on an unselected tab of a TabControl, until the region closes, the window closes, a guarding host loads with the region in another window, or a host of the region turns this off. The window holds no host, and holds its regions weakly.
While a guarded region has entries, the window's close is cancelled and the regions are cleared one after another, in the order their hosts first loaded; the window closes when every clear commits. A guard that rejects, a cancelled clear or a superseded clear keeps the window open with its entries and asks no further regions, so a guard can confirm through a NavigationDialogHost in the same window. A close while a clear is still in flight joins it; a close after the clearing region stops transitioning starts a new check. A close that another Window.Closing handler already cancelled is left alone, and a window whose regions are empty closes at once.
WPF ignores cancellation when Application.Shutdown closes the windows, and doesn't raise Window.Closing when the Windows session ends: those paths don't ask the guards. Close the main window instead of calling Shutdown to give the guards a say, and handle Application.SessionEnding if a draft must survive a log-off.
HandlesBrowseBack
public bool HandlesBrowseBack { get; set; } Gets or sets whether NavigationCommands.BrowseBack goes back in the region. The default is true.
IsBusy
public bool IsBusy { get; } Gets whether a request of the region has not completed (NavigationRegion.IsBusy).
IsTransitioning
public bool IsTransitioning { get; } Gets whether the region has a transition in flight.
RetainViews
public bool RetainViews { get; set; } Gets or sets whether the host keeps the view of each retained entry it presented and shows it again when the entry becomes current again, instead of asking for a new view. The default is false. The property is inherited: set on a window or a container, it applies to every host below it, including the hosts in kept views.
Remarks
The host keeps the entry's ContentPresenter with its view and FrameworkElement.DataContext, so state that lives only in the view, such as scroll positions, a text box's caret and selection, expanded tree nodes or a grid's sort order and column widths, survives Back and BackTo. The locator isn't asked again for a kept view. A view is never shown for two entries: an instance pushed twice gets two entries and two views. When keyboard focus was inside the view as it left, the host focuses that element again once the view has loaded again, unless focus has moved elsewhere outside the host meanwhile; in an inactive window it only sets the element as the window's focused element.
An entry pushed with NavigationRequestOptions.RetainView set (NavigationEntry.RetainView) uses that value instead of this property: true keeps its view also when this property is false, and false never keeps it. The rules below apply to each entry with the value that decides for it.
The host drops a kept view when it sees that its entry has left the region's stack (the entry retired, also while its content's disposal still waits for its operations), when the region empties, when NavigationHost.Region changes, and when it presents an entry while the value that decides for the kept view's entry is false. It doesn't dispose views. An unloaded host doesn't follow its region: it keeps its views, also its current one, and when it loads again with the same region it shows the same view if the current entry is unchanged, and drops the views of entries that retired meanwhile. So the hosts inside a kept view keep their views too. A host that is never loaded again is collected with its views.
A kept view leaves and re-enters the visual tree, so its FrameworkElement.Unloaded and FrameworkElement.Loaded events are raised again. Each retained entry keeps its visual tree alive, and a view built by a locator keeps the entry-scoped services it was given, until the host drops it. An inner host can set the property to false to opt out.
ViewLocator
public INavigationViewLocator? ViewLocator { get; set; } Gets or sets the locator asked first for each entry's view.
Methods
GetRetainViews
public static bool GetRetainViews(DependencyObject element) Gets the inherited NavigationHost.RetainViews value of an element.
Parameters
elementThe element.
Returns
Whether the hosts at and below the element keep the views of retained entries.
SetRetainViews
public static void SetRetainViews(DependencyObject element, bool value) Sets NavigationHost.RetainViews for the hosts at and below an element, such as a window.
Parameters
elementThe element.
valueWhether the hosts keep the views of retained entries.