[Prism] Navigate

1. Main functions

        Provides the function of jumping to different pages in the same area.

Two, realize

        First register the navigation area in the App class:

            containerRegistry.RegisterForNavigation<ViewA>();
            containerRegistry.RegisterForNavigation<ViewB>();
            containerRegistry.RegisterForNavigation<ViewC>();
            containerRegistry.RegisterForNavigation<ViewD>();
            containerRegistry.RegisterForNavigation<ViewE>();

        Then set the navigation area in the Xaml file:

<ContentControl Grid.Column="1" prism:RegionManager.RegionName="xRegion"/>

        After that add property in ViewModel:

private readonly IRegionManager regionManager;

         Initialize the regionManager property in the ViewModel constructor

        public MainViewModel(RegionManager regionManager )
        {
            this.regionManager=regionManager;
        }

         last to navigate

regionManager.RequestNavigate("xRegion", "ViewA");

3. Parameter passing

        RequestNavigate provides several parameter transfers:

        void RequestNavigate(string regionName, string target, Action<NavigationResult> navigationCallback, NavigationParameters navigationParameters);

Mainly these four:

        regionName: the region name of the navigation

        target: the name of the navigation View, because it has been registered before, so the direct string

        navigationCallback: the callback function of the navigation, which is called when the navigation ends

        navigationParameter: Navigation parameters (passing form is similar to DialogResult)

4. Navigation log

        journal can provide previous navigation log records, calling GoForward and GoBack can forward and backward the previous navigation log records. The main implementation is as follows:

        First add the journal attribute:

public IRegionNavigationJournal journal;

        Then the journal is initialized, and there is a navigateCallback attribute in the previous navigation method RequestNavigate()   

regionManager.RequestNavigate("theRegion", "SecondView", arg =>
{
    journal = arg.Context.NavigationService.Journal;
});

        Then call the journal's forward and backward methods in the navigation command

        public DelegateCommand GoCommand
        {
            get => new DelegateCommand(() =>
            {
                journal.GoForward();
            });
        }
        public DelegateCommand BackCommand
        {
            get => new DelegateCommand(() =>
            {
                journal?.GoBack();
            });
        }

 GoCommand and BackCommand are bound to the two navigation buttons in the foreground, so that the navigation log function can be completed

Guess you like

Origin blog.csdn.net/weixin_43163656/article/details/127904798