- 定义导航发生时执行特定的操作
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1.ViewModels
{
public class SettingViewModel : BindableBase,INavigationAware
{
#region 私有字段区
#endregion
#region 属性区
private string _title = "setting";
public string Title
{
get { return _title; }
set
{
SetProperty(ref _title, value);
}
}
#endregion
#region 命令区
//public ICommand LoginCommand
//{
// get => new DelegateCommand<Window>((window) =>
// {
// });
//}
#endregion
public SettingViewModel(IRegionManager regionManager)
{
}
private void LoadMenuItems()
{
}
//导航时,是否重用原来的实例
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//导航到此页面的时候
public void OnNavigatedTo(NavigationContext navigationContext)
{
if (navigationContext.Parameters.ContainsKey("Title"))
{
Title= navigationContext.Parameters.GetValue<string>("Title");
}
}
//导航离开此页面的时候,用于拦截离开的请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
}
}
- 触发导航行为,并携带参数
//_regionManager.RequestNavigate("ContentRegion", value.NavigationTarget);
NavigationParameters navigationParameters = new NavigationParameters();
navigationParameters.Add("Title", DateTime.Now.ToString());
_regionManager.RequestNavigate("ContentRegion", "SettingView", navigationParameters);
3 添加确认和导航日志
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1.ViewModels
{
public class SettingViewModel : BindableBase,IConfirmNavigationRequest
{
#region 私有字段区
#endregion
#region 属性区
private string _title = "setting";
public string Title
{
get { return _title; }
set
{
SetProperty(ref _title, value);
}
}
#endregion
#region 命令区
//public ICommand LoginCommand
//{
// get => new DelegateCommand<Window>((window) =>
// {
// });
//}
#endregion
public SettingViewModel(IRegionManager regionManager)
{
}
private void LoadMenuItems()
{
}
//导航时,是否重用原来的实例
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//导航到此页面的时候
public void OnNavigatedTo(NavigationContext navigationContext)
{
if (navigationContext.Parameters.ContainsKey("Title"))
{
Title= navigationContext.Parameters.GetValue<string>("Title");
}
}
//导航离开此页面的时候,用于拦截离开的请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
//离开确认,用于拦截离开的请求
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
bool result = true;
if (MessageBox.Show("确认离开该页面", "温馨提示", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
result=false;
}
continuationCallback(result);
}
}
}
主窗口视图模型上
public ICommand BackCommand
{
get => new DelegateCommand(() =>
{
if(journal!=null&&journal.CanGoBack)
{
journal.GoBack();
}
});
}
public ICommand ForwardCommand
{
get => new DelegateCommand(() =>
{
if (journal != null && journal.CanGoForward)
{
journal.GoForward();
}
});
}
4 INavigationAware介绍
INavigationAware是Prism框架中的一个接口,用于定义视图模型(ViewModel)是否支持导航,并提供一些与导航相关的方法。通过实现INavigationAware接口,可以让视图模型能够在导航发生时执行特定的操作。
INavigationAware接口定义了以下方法:
OnNavigatedTo:当视图模型导航到时调用。可以在该方法中执行一些初始化操作或处理导航参数。
OnNavigatedFrom:当视图模型导航离开时调用。可以在该方法中执行清理操作或保存视图模型的状态。
IsNavigationTarget:确定当前视图模型是否是导航的目标。在导航期间,Prism会调用这个方法来判断是否创建新的实例,或者重用已有的实例。
下面是一个使用INavigationAware的视图模型示例:
public class MyViewModel : BindableBase, INavigationAware
{
public void OnNavigatedTo(NavigationContext navigationContext)
{
// 在导航到该视图模型时执行的逻辑
var parameter = navigationContext.Parameters["ParameterName"];
// 执行相关操作
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
// 在导航过程中判断当前视图模型是否是导航的目标
var parameter = navigationContext.Parameters["ParameterName"];
// 返回是否是目标的结果
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
// 在导航离开该视图模型时执行的逻辑
// 清理操作或保存视图模型的状态等
}
}
要使用INavigationAware,需要在视图模型中实现该接口,并根据需要实现相关方法。然后,在使用Prism进行导航时,Prism框架会按照一定的流程调用这些接口方法,以便执行相应的操作或判断。