WPF Prism Framework (Navigation)

1.Background

Navigation is to achieve switching between different interfaces and is a way to organize system functions. Two types of navigation are provided, one is based on View switching, and the other is based on ViewModel state.

2. Navigation application

2.1 Navigation switching

First, introduce the Prism framework according to the method introduced in Prism (MVVM)

Then create the three projects shown in the diagram, introduce the Prism framework respectively, and create the corresponding folders

 The specific code is the same as mentioned in the WPF Prsim Framework (Region) article. For details, please see the WPF Prsim Framework (Region) article.

2.2 Navigation parameter transfer

To pass navigation parameters, you must inherit the INavigationAware interface in the received ViewModel class. After inheriting the interface, you must implement three methods. The execution order of the methods is as explained in the comments:

   //第一次从main进入pageA 先执行构造器,在执行OnNavigatedTo;
        //第二种情况PageA到PageA OnNavigatedFrom ==> IsNavigationTarget ==> OnNavigatedTo

        //导航目标
        //OnNavigatedTo() 一般用于接收导航参数 navigationContext导航上下文
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            try
            {
                Name = navigationContext.Parameters.GetValue<string>("Name");
                Sex = navigationContext.Parameters.GetValue<bool>("Sex");
                Id = Convert.ToInt32(navigationContext.Parameters["Id"]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

//控制每次重新导航时,是否创建当前实例的PageAviewModel,当为true时不在重新创建当前实例,为false时重新创建当前实例
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

//导航的来源

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
     
        }

The parameter passing method is in MainViewModel

    public MainViewModel(IRegionManager regionManager, IRegionNavigationJournal journal)
        {
            OpenCommnd = new DelegateCommand<string>(Open);
            BackCommnd = new DelegateCommand(Back);
            Journal = journal;
            iRegionManager = regionManager;
        }

        private void Open(string obj)
        {
            if (obj.ToString() == "PageB")
            {
                iRegionManager.Regions["con"].RequestNavigate(obj);
            }
            else
            {
//定义导航参数
                NavigationParameters par = new NavigationParameters
                {
                    { "Id", 5 },
                    { "Name", "111" },
                    { "Sex", true }
                };

//通过RequestNavigate()的第三个参数进行传参
//第一个参数 obj为注册的窗体,用户控件或者页面
//第二个参数 跳转时,记录的导航日志

                this.iRegionManager.Regions["con"].RequestNavigate(obj, (callback) =>
                {
                    Journal = callback.Context.NavigationService.Journal;
                }, par);
            }
        }

When passing parameters and receiving parameters, pay attention to the accuracy of the key values ​​passed.

2.3 Navigation interception

Navigation interception must also inherit the IConfirmNavigationRequest interface in the received ViewModel class. The IConfirmNavigationRequest interface inherits the INavigationAware interface.

Implement one more method than passing parameters

 public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            bool result = true;
            if (Id==6)
            {
                result = false;
            }
            continuationCallback(result);
        }

continuationCallback(bool) calls back a bool value. If it is true, it is allowed to enter the next interface, if it is false, it is intercepted.

2.4 Navigation log

//通过RequestNavigate()的第三个参数进行传参
//第一个参数 obj为注册的窗体,用户控件或者页面
//第二个参数 跳转时,记录的导航日志

                this.iRegionManager.Regions["con"].RequestNavigate(obj, (callback) =>
                {
//callback.Context当前导航的上下文
//NavigationService 导航服务
//Journal 导航日志
                    Journal = callback.Context.NavigationService.Journal;
                }, par);
            }
        }
  private void Back()
        {
            if (Journal.CanGoBack)
            {
//如果有返回的日志,goback返回,GoForward() 前进 Clear() 清除日志
                Journal.GoBack();
            }
        }

 Bind the button to implement log rollback 

 BackCommnd = new DelegateCommand(Back);

Guess you like

Origin blog.csdn.net/qq_57212959/article/details/132027278