Prism Demo系列(二十) Prism架构剖析:20-NavigateToExistingViews

内容、结构和上篇相同,只有部分ViewAViewModel.cs或ViewBViewModel.cs是不同的:

ViewBViewModel.cs:

using Prism.Mvvm;
using Prism.Regions;

namespace ModuleA.ViewModels
{
    public class ViewBViewModel : BindableBase, INavigationAware
    {
        private string _title = "ViewB";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private int _pageViews;
        public int PageViews
        {
            get { return _pageViews; }
            set { SetProperty(ref _pageViews, value); }
        }

        public ViewBViewModel()
        {

        }


        /// <summary>
        /// 一旦导航内容改变(即navigationContext的上下文,随着navigatePath的内容),
        /// 会立即执行下面的函数。OnNavigatedTo()是对接口INavigationAware的实现。
        /// Called when the implementer has been navigated to(导航内容来到这里时,则执行该函数)
        /// </summary>
        /// <param name="navigationContext"></param>
        public void OnNavigatedTo(NavigationContext navigationContext)
        //public void OnNavigatedTo()//传输参数为空,编译出错,必然不会执行PageViews++。
        {
            PageViews++;
        }

        /// <summary>
        /// false:当前实例的不能操作导航内容,会创建另外一个ViewB的实例
        /// PageViews==6时,返回false。
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <returns></returns>
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return PageViews / 6 != 1;
        }

        /// <summary>
        ///  Called when the implementer is being navigated away from.(导航内容离开时,则执行该函数)
        /// </summary>
        /// <param name="navigationContext"></param>
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}

点击六次按钮B后,会添加另一个页面:

这部分内容有点复杂,自己慢慢调试即可。 

猜你喜欢

转载自blog.csdn.net/xpj8888/article/details/86554304