调用IRegionNavigationJournal对象中的方法,来实现对导航的日志管理
导航日志就是对导航系统的一个管理的功能,我们一般情况下会知道我们上一步导航的 位置,回到上一步以后,我们又会知道下一步导航的位置,我们整个导航的历史记录,以便于我们可以灵活的对我们的应用程序进行控制
IRegionNavigationJournal该接口下包含下面的方法
GoBack():返回上一页
CanGoBack:是否可以返回上一页
GoForward():返回后一页
CanGoForward():是否可以返回后一页
通过之前的实现IConfirmNavigationRequest接口之后,在方法中实现跳转请求
//允许用户针对导航请求进行拦截
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
continuationCallback(MessageBox.Show("你确定要离开这个页面吗", "跳转导航", MessageBoxButton.YesNo) == MessageBoxResult.Yes);
}
实现跳转请求后,如何实现导航的日志记录,跳转后再跳转回上一页,再从上一页再次跳转回当前页
首先在主窗体加载中调用IRegionNavigationJournal对象
private IRegionNavigationJournal regionNavigationJournal;
创建导航命令,用于前端的绑定
//导航命令
public DelegateCommand<string> NavigationCommand{ get; set; }
//导航命令,下一页命令
public DelegateCommand GoForwardCommand { get; set; }
//导航命令,上一页命令
public DelegateCommand GoBackCommand { get; set; }
创建一个导航方法,在方法中进行导航时,为regionNavigationJournal对象赋值
//导航方法
public void Navigation(string viewName) {
_regionManager.RequestNavigate("ContentRegion", viewName, callback => {
regionNavigationJournal = callback.Context.NavigationService.Journal;
});
}
在主窗体的构造函数中,将导航方法指向到导航命令中,实现匿名函数 为GoForwardCommand,GoBackCommand两个导航命令赋予导航方法,判断是否可以返回上一页已经回退下一页
public MainWindowViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
NavigationCommand =new (Navigation);
GoForwardCommand = new(() => {
//表示当regionNavigationJournal不为空且为CanGoForward
if (regionNavigationJournal is { CanGoForward: true }) {
regionNavigationJournal.GoForward();
}
});
GoBackCommand = new(() => {
if (regionNavigationJournal is { CanGoBack: true }) {
regionNavigationJournal.GoBack();
}
});
}
在MainWindows中绑定command
<Button Width="60" Height="30" Command="{Binding NavigationCommand}" CommandParameter="ViewA">ViewA</Button>
<Button Width="60" Height="30" Command="{Binding NavigationCommand}" CommandParameter="ViewB">ViewB</Button>
<Button Width="60" Height="30" Command="{Binding GoBackCommand}">上一页</Button>
<Button Width="60" Height="30" Command="{Binding GoForwardCommand}" >下一页</Button>
匿名函数部分也可以写成这样
private IRegionManager _regionManager;
private IRegionNavigationJournal regionNavigationJournal;
//导航命令
public DelegateCommand<string> NavigationCommand{ get; set; }
//导航命令,下一页命令
public DelegateCommand GoForwardCommand { get; set; }
//导航命令,上一页命令
public DelegateCommand GoBackCommand { get; set; }
//导航方法
public void Navigation(string viewName) {
_regionManager.RequestNavigate("ContentRegion", viewName, callback => {
regionNavigationJournal = callback.Context.NavigationService.Journal;
});
}
private void GoForwardCommandMethod() {
if (regionNavigationJournal is { CanGoForward: true })
{
regionNavigationJournal.GoForward();
}
}
private void GoBackCommandMethod() {
if (regionNavigationJournal is { CanGoBack: true })
{
regionNavigationJournal.GoBack();
}
}
public MainWindowViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
NavigationCommand =new (Navigation);
GoForwardCommand = new(GoForwardCommandMethod);
GoBackCommand = new(GoBackCommandMethod);
}
原文地址:https://blog.csdn.net/qq_54077266/article/details/142979477
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!