使用INavigationService而不丢弃ViewModels?

时间:2017-02-23 14:28:58

标签: c# wpf mvvm user-controls devexpress

我正在使用[{'views': '9', 'entries': '5', 'time': 'October 1'}, {'views': '3', 'entries': '3', 'time': 'October 2'}] ,因此我可以在两个ViewModel之间导航,如下所示:

MasterViewModel:

INavigationService

SubViewModel:

 public virtual INavigationService NavigationService { get; }
 //Command Navigate to SubUserControlView:
 NavigationService.Navigate("SubUserControlView", parentViewModel: this);

当我返回 public virtual INavigationService NavigationService { get; } //Command Navigate back to MainUserControlView: NavigationService.GoBack(parameter); 时,我的所有更改都将被丢弃。我怎么能避免这种情况?我想使用MasterViewModel从列表中选择一个值并将其返回SubUserControlView

1 个答案:

答案 0 :(得分:1)

尝试将主KeepAlive属性设置为true

<Page x:Class="WpfApplication3.MasterPage"
      .... KeepAlive="True">

这样可以防止每次导航到https://msdn.microsoft.com/en-us/library/system.windows.controls.page.keepalive(v=vs.110).aspx时都会创建Page的新实例。

这也会导致您的视图模型不能重新创建,假设您在页面的构造函数中创建它们。