In Prism, using Navigation to jump between pages usually requires the following steps:
- Create a new page and implement the INavigationAware interface
- Use IRegionManager to register pages
- Use NavigationParameters to encapsulate the parameters of the page jump
- Using IRegionManager. RequestNavigate () to jump to the target page
INavigationAware interface
INavigationAware has three interfaces, IsNavigationTarget, OnNavigatedFrom, and OnNavigatedTo, which will start when the page is navigated. You can pre-interface methods as required, as shown in the code below: Prism Base class page
The sample code is shown below
public class RegionViewModelBase : ViewModelBase, INavigationAware, IConfirmNavigationRequest
{
protected IRegionManager RegionManager { get; private set; }
public RegionViewModelBase(IRegionManager regionManager)
{
RegionManager = regionManager;
}
public virtual void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
continuationCallback(true);
}
public virtual bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public virtual void OnNavigatedFrom(NavigationContext navigationContext)
{}public virtual void OnNavigatedTo(NavigationContext navigationContext)
{}}Copy the code
The navigated page can inherit from RegionViewModelBase, and the code below shows overriding the OnNavigatedTo method to start the related event method in OnNavigatedTo.
public class ViewAViewModel : RegionViewModelBase
{
private string _message;
public string Message
{
get { return _message; }
set{ SetProperty(ref _message, value); }}public ViewAViewModel(IRegionManager regionManager, IMessageService messageService) :
base(regionManager)
{
Message = messageService.GetMessage();
}
public override void OnNavigatedTo(NavigationContext navigationContext)
{
//do something}}Copy the code
Navigation code implementation
As shown in the code below, the navigation method is RequestNavigate with the parameters (region, source)
- The area is the area where the page navigates to the display page
- Source refers to the name of the page when it is instantiated
OnInitialized is the module initialization method
RegisterTypes is the registered project class method
Both methods are executed automatically when the module is loaded. _regionManager.RequestNavigate(RegionNames.ContentRegion, “ViewA”); The code shows that when initialized, the ViewA page is navigated to the ContentRegion region of the main page.
public class ModuleNameModule : IModule
{
private readonly IRegionManager _regionManager;
public ModuleNameModule(IRegionManager regionManager)
{
_regionManager = regionManager;
}
/// <summary>
/ / / initialized
/// </summary>
/// <param name="containerProvider"></param>
public void OnInitialized(IContainerProvider containerProvider)
{
_regionManager.RequestNavigate(RegionNames.ContentRegion, "ViewA");
}
/// <summary>
/// register the page to the container
/// </summary>
/// <param name="containerRegistry"></param>
public void RegisterTypes(IContainerRegistry containerRegistry)
{ containerRegistry.RegisterForNavigation<ViewA>(); }}Copy the code