【发布时间】: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 的视图都可以被导航离开。
【问题讨论】: