【问题标题】:INavigationAware can't navigate awayINavigationAware 无法导航
【发布时间】:2018-04-12 17:04:57
【问题描述】:

我正在使用 Prism Unity,我有一个抽象 RecordViewModel:BindableBase、RecordListViewModel:RecordViewModel 和 RecordUpdateViewModel:RecordViewModel、INavigationAware。还有一个单独的导航模块,我的 MainWindow 有 2 个区域,NavigationRegion 和 ContentRegion。所有 RecordView 都驻留在 ContentRegion 中。无论出于何种原因,无论是制作 GoBack 按钮还是单击 NavigationRegion 中的按钮,我都无法离开 Update 视图。我已经缩小了问题出在 ViewModel 中的范围,并且我缺少 INavigationAware 的一些东西。请告诉我我遗漏了什么或做错了什么,谢谢。

public class RecordUpdateViewModel : RecordViewModel, INavigationAware
{
    private IRegionNavigationJournal navigationJournal;

    public RecordUpdateViewModel(IRecordService context) : base(context)
    {
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return false;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
        throw new NotImplementedException();
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        //irrelevant to problem logic to bring in Record.Id

        navigationJournal = navigationContext.NavigationService.Journal;
    }
}

编辑

以防我在其他地方搞砸了,这是我在 module.cs 中的注册

container.RegisterType<IRecordService, RecordService>();

container.RegisterTypeForNavigation<RecordListView>();
container.RegisterTypeForNavigation<RecordUpdateView>();

regionManager.RegisterViewWithRegion("ContentRegion", typeof(RecordListView));
regionManager.RegisterViewWithRegion("ContentRegion", typeof(RecordUpdateView));

我使用 regionManager.RequestNavigate("ContentRegion", "RecordUpdateView", parameter) 导航到视图,如果我不在 UpdateView 上使用 INavigationAware,所有按钮都可以工作,但是当我重新打开时我不能离开。

编辑2

这是导航到 UpdateView 和绑定命令的 ListView 的 XAML

<UserControl x:Class="App.Record.Views.RecordListView"                  
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <DockPanel>
        <WrapPanel>
            <Button Content="Edit"
                    Command="{Binding EditCommand}"/>
        </WrapPanel>
        <DataGrid>
           <--Irrelevant-->
        </DataGrid>
    </DockPanel>
</UserControl>

命令

private DelegateCommand editCommand;
public DelegateCommand EditCommand => editCommand ?? (editCommand = new DelegateCommand(EditRecord));

private const string RecordID = "RecordID";
void EditCommand()
{
    var parameter = new NavigationParameters();
    parameter.Add("RecordID", SelectedRecord.ID);
    regionManager.RequestNavigate("ContentRegion", "RecordUpdateView", parameter);
}

导航菜单按钮的命令工作方式相同,任何不使用 INavigationAware 的视图都可以被导航离开。

【问题讨论】:

    标签: c# wpf prism


    【解决方案1】:

    INavigationAware 与从一个视图导航到另一个视图无关。它只是为您提供了一种在视图之间传递参数的简单方法。我认为您正在寻找的是称为IRegionManager 的东西,然后您可以执行类似regionManager.NavigateTo(regionName,viewName) 的操作,您将必须使用您的容器注册视图。 container.RegisterType&lt;object,Views.View&gt;(ViewNames.ViewName);ViewNames.ViewName 之类的东西是带有视图名称的常量字符串。

    【讨论】:

    • 我编辑了我的问题以提供更多细节,我确实认为 INavigationAware 似乎不太正确,但希望更多的 sn-ps 会导致某人解决问题。
    • 可以添加 xaml 吗?
    • 我添加了更多的sn-ps,你是不是觉得我的命令有问题?
    • 不,我认为您需要注册您的区域。我认为您的区域应该看起来像&lt;ContentControl prism:RegionManager.RegionName="{x:Static constants:RegionNames.MyRegion}"&gt;&lt;/ContentControl&gt;
    • 然后拨打regionManager.RequestNavigate(regionName,viewName);
    【解决方案2】:

    我会羞愧地低下头...查看 OnNavigatedFrom... 和所有 3 个 INavigationAware 成员以寻找其他任何人...并删除自动生成的 NotImplementedExceptions。如果你不需要那个空隙,它必须在那里,这样 INavigationAware 就不会因为你没有完全实现它而对你大喊大叫,但是如果你不需要它们,这些空隙中不必有任何逻辑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 2015-04-26
      相关资源
      最近更新 更多