》》》主程序
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Navigation.Regions;
using System;namespace BlankApp2.ViewModels
{public class MainViewModel : BindableBase{private string _title = "Prism Application";public string Title{get { return _title; }set { SetProperty(ref _title, value); }}private readonly IRegionManager _regionManager;//区域导航日志private IRegionNavigationJournal _journal;public DelegateCommand<string> OpenCommand { get; private set; }public DelegateCommand BackCommand { get; private set; }public MainViewModel(IRegionManager regionManager){this._regionManager = regionManager;this.OpenCommand = new DelegateCommand<string>(Open);this.BackCommand = new DelegateCommand(Back);}private void Back(){if (_journal.CanGoBack){_journal.GoBack();//_journal.GoForward();}}private void Open(string obj){INavigationParameters keyValuePairs = new NavigationParameters();keyValuePairs.Add("zen", "====zen=============");_regionManager.Regions["ContentRegion"].RequestNavigate(obj, callback ,keyValuePairs);}private void callback(NavigationResult result){if (result.Success) { this._journal=result.Context.NavigationService.Journal;}}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace ModuleA.ViewModels
{//IConfirmNavigationRequest 继承 INavigationAware public class ViewAViewModel: BindableBase , IConfirmNavigationRequest{private string navValue;public string NavValue{get { return navValue; }set { navValue = value;RaisePropertyChanged();}}public ViewAViewModel(){}public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback){//throw new NotImplementedException();//拦截切换导航bool result = false;if (MessageBox.Show("确认导航?","Tip:",MessageBoxButton.YesNo)==MessageBoxResult.Yes){result = true;}continuationCallback(result);}/// <summary>/// 首次不触发,再次导航时,是否重用原来的实例/// 调用时机: 在导航发生时,Prism会询问当前实例是否应该被重用作为导航目标./// IsNavigationTarget()方法用来空时视图或者视图模型实例在导航时是否被重用./// 当你尝试导航到一个新的视图的时候,Prism会检查当前活动的视图或者视图模型的实例/// ,并调用他们的IsNavigationTarget方法,以决定是否可以重用这个实例.如果返回true,/// prism框架将不会创建新的视图或者视图模型实例,而是重用当前的实例./// 这种方式非常适用于那些数据和状态不需要每次都重新加载的视图,可以提高应用程序的响应速度和资源利用率./// </summary>/// <param name="navigationContext"></param>/// <returns></returns>public bool IsNavigationTarget(NavigationContext navigationContext){return true;}/// <summary>/// 当从当前视图或者视图模型导航离开时,该方法被调用. 就是比如你从ViewC导航到ViewB的时候,/// 如果导航成功[ConfirmNavigationRequest],就是要打开ViewB以及离开ViewC的时候,/// ViewC对应的ViewCModel中的OnNavigationFrom()会被调用.///作用就是允许你在视图或者视图模型不再是导航目标后执行清理或者保存状态的代码.///这是保存当前视图状态或者是取消订阅事件的好地方/// </summary>/// <param name="navigationContext"></param>public void OnNavigatedFrom(NavigationContext navigationContext){//throw new NotImplementedException();}/// <summary>/// 导航到某个视图//调用时机: 当导航到某个试图或者视图模型时,该方法被调用,比如导航到ViewA的时候ViewAModel中的这个方法就会被调用.//作用就是允许你在视图或者是视图模型成为导航目标后执行代码.比如处理参数以及做一些初始化的工作./// </summary>/// <param name="navigationContext"></param>public void OnNavigatedTo(NavigationContext navigationContext){if (navigationContext.Parameters.ContainsKey("zen")){this.NavValue = navigationContext.Parameters.GetValue<string>("zen");}//throw new NotImplementedException();}}
}
源码