使用 MVVM 的棱镜导航 GoBack()

时间:2021-03-05 10:40:41

标签: wpf mvvm navigation prism

我在创建 GoBack 按钮时遇到问题(按预期工作:))。

我正在使用 MVVM 并且我有一个名为 ContentRegion 的区域。我的 MainWindow 显示了返回按钮和区域,就是这样。假设我只有 ViewAViewB。我从 ViewAViewB 的基本导航工作正常。在 MainWindow 中,我在启动时将区域设置为 ViewA,并且我将 DelegateCommand 用于 GoBack 按钮以及 GoBack()CanGoBack() 方法:

public MainWindowViewModel(IRegionManager regionManager)
{
    _regionManager = regionManager;

    _regionManager.RegisterViewWithRegion("ContentRegion", typeof(AddCorpActView));

    NavigateCommand = new DelegateCommand(GoBack, CanGoBack);

}

private void GoBack()
{
    _regionManager.Regions["ContentRegion"].NavigationService.Journal.GoBack();
}

private bool CanGoBack()
{
    return true;
}

ViewModelAViewModelB 中,我添加了属性:

private IRegionNavigationService _navigationService;

public void OnNavigatedTo(NavigationContext navigationContext)
{
    _navigationService = navigationContext.NavigationService;
}

当我使用 GoBack 按钮从 ViewB 转到 ViewA 时,我没有收到任何错误,但也没有任何反应。

任何输入将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题是这一行,您使用视图发现来分配初始视图。

_regionManager.RegisterViewWithRegion("ContentRegion", typeof(AddCorpActView))

视图发现和视图注入不能与期刊see documentation结合使用。

<块引用>

导航日志只能用于区域导航服务协调的基于区域的导航操作。如果使用视图发现或视图注入实现区域内导航,导航日志不会在导航过程中更新,也无法用于在该区域内向前或向后导航。

改为仅使用导航服务。通过调用导航服务替换通过视图发现设置初始视图,例如:

_regionManager.RequestNavigate("ContentRegion", ...)

您还可以将使用导航服务的初始视图设置移动到您的模块定义中。