wpf 棱镜导航 - 上一个和下一个视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25948219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:33:23  来源:igfitidea点击:

Prism navigation - previous and next view

c#wpfmvvmprism-4

提问by DimaK

I'm developing a wpf application using prism and MVVM.

我正在使用棱镜和 MVVM 开发 wpf 应用程序。

I have a main shell, which has two regions: Menu region and Main region.

我有一个主 shell,它有两个区域:菜单区域和主区域。

I'm trying to implement within the menu region (which contains the MenuView) a BACK and FORWARD buttons, exactly as we have in any browser: Let's say I have 5 views: View1, View2, View3, View4, View5.
When I launch the application, View1 is shown in the Main Region. For the current moment, I want those buttons to be disabled. Now, I choose to navigate to View3: View3 is shown in the Main Region, and then the Back btn becomes enabled (the forward btn is remained disabled). Then, I navigate to View2, and it is displayed in Main Region.

我试图在菜单区域(包含 MenuView)中实现一个后退和前进按钮,就像我们在任何浏览器中一样:假设我有 5 个视图:View1、View2、View3、View4、View5。
当我启动应用程序时,View1 显示在主区域中。目前,我希望禁用这些按钮。现在,我选择导航到 View3:View3 显示在 Main Region 中,然后 Back btn 被启用(forward btn 保持禁用状态)。然后,我导航到 View2,它显示在 Main Region 中。

Now, when I click Back btn, View 3 is displayed in Main Region, and the Forward btn becomes enabled. I click Forward, and Now View 2 is displayed, and Forward btn becomes disabled.

现在,当我单击 Back btn 时,View 3 显示在 Main Region 中,并且 Forward btn 变为启用状态。我单击转发,现在显示视图 2,并且转发 btn 被禁用。

I've tried using the Navigation Journal, as explained in the following link: http://msdn.microsoft.com/en-us/library/gg430861(v=pandp.40).aspx

我试过使用导航日志,如以下链接所述:http: //msdn.microsoft.com/en-us/library/gg430861(v=pandp.40).aspx

But had no success, because I did what they mentioned in the MenuViewModel, which is the only view that is dispayed in the Menu region for the whole application life (and only Main Region switches views). That's why, OnNavigatedTo method is never called, cause I never nevigate to the MenuView, and this causes navigationService to always be null.

但是没有成功,因为我做了他们在 MenuViewModel 中提到的事情,这是整个应用程序生命周期中菜单区域中唯一显示的视图(并且只有主区域切换视图)。这就是为什么从不调用 OnNavigatedTo 方法,因为我从不导航到 MenuView,这导致 navigationService 始终为 null。

The main point is that I want these buttons in the MenuView - the only view displayed in the Menu Region for all the application life. And the Back and forward buttons navigate between views in the Main Region - back and forth. Would appreaciate your suggestions.

主要的一点是我希望 MenuView 中的这些按钮 - 在整个应用程序生命周期中显示在菜单区域中的唯一视图。后退和前进按钮在主区域的视图之间导航 - 来回。会感谢您的建议。

回答by DimaK

This is how I solved it:

我是这样解决的:

From the MenuViewModel, I have a reference to the RegionManager, so I can get access to the main region and it's navigation service:

从 MenuViewModel 中,我引用了RegionManager,因此我可以访问主要区域及其导航服务:

var mainregion = _regionManager.Regions[RegionNames.mainregion];
mainregion.NavigationService.Journal.GoForward();

回答by pejman

you can use bottom code for previous or forward region

您可以为前一个或前一个区域使用底部代码

xml file

xml文件

<Button Command="{Binding GoBackCommand}" Content="GoBack" />

ViewModel C#

视图模型 C#

private readonly IRegionManager _regionManager;
public ICommand GoBackCommand { get; set; }
public ClassName(IRegionManager regionManager)
{
    _regionManager = regionManager;
    GoBackCommand = new DelegateCommand(GoBack, CanGoBack);
}

private bool CanGoBack()
{  return _regionManager.Regions[RegionNames.MainRegion].NavigationService.Journal.CanGoBack;/*or CanGoForward */}

private void GoBack()
{ _regionManager.Regions[RegionNames.MainRegion].NavigationService.Journal.GoBack();/*GoForward()*/ }

回答by jeevan

var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
var moduleAView = new Uri("ModuleAContentView", UriKind.Relative);
regionManager.RequestNavigate("ContentRegion", moduleAView);

you can navigate between region by above code

您可以通过上面的代码在区域之间导航