如何在MVVM中实现OnNavigatedTo / From?

时间:2021-01-03 14:27:37

I would like my ModelView to know when navigated page has been activated/deactivated so I found the following article, which has nice way to handle above. The bad thing is this article is targeted to WinRT and my application is neither WinRT nor Silverlight. So I have to use whatever System.Windows.Controls.Page provides. How can I implement such mechanism?

我希望我的ModelView知道导航页面何时被激活/停用,所以我找到了以下文章,它有很好的方法来处理上面的内容。不好的是这篇文章的目标是WinRT,我的应用程序既不是WinRT也不是Silverlight。所以我必须使用System.Windows.Controls.Page提供的任何内容。我该如何实现这样的机制?

1 个解决方案

#1


0  

Are you looking for tracking the navigation events? I think you need to implement the INavigationAware interface either in your View or your ViewModel (Prism will check first the view, and if it doesn't implement INavigationAware it will also check the ViewModel).

您是否在寻找跟踪导航事件?我认为您需要在View或ViewModel中实现INavigationAware接口(Prism将首先检查视图,如果它没有实现INavigationAware,它也将检查ViewModel)。

Please see a short example of its implementation, below. But the thing is you need to implement all the 2 methods namely OnNavigatedFrom and OnNavigatedTo

请参阅下面的实施简短示例。但问题是你需要实现所有2种方法,即OnNavigatedFrom和OnNavigatedTo

public class MyViewModel : INavigationAware 
{
   bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
  {
     return false;
  }

  void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
  {
  }

  void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
  {
  }
}

#1


0  

Are you looking for tracking the navigation events? I think you need to implement the INavigationAware interface either in your View or your ViewModel (Prism will check first the view, and if it doesn't implement INavigationAware it will also check the ViewModel).

您是否在寻找跟踪导航事件?我认为您需要在View或ViewModel中实现INavigationAware接口(Prism将首先检查视图,如果它没有实现INavigationAware,它也将检查ViewModel)。

Please see a short example of its implementation, below. But the thing is you need to implement all the 2 methods namely OnNavigatedFrom and OnNavigatedTo

请参阅下面的实施简短示例。但问题是你需要实现所有2种方法,即OnNavigatedFrom和OnNavigatedTo

public class MyViewModel : INavigationAware 
{
   bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
  {
     return false;
  }

  void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
  {
  }

  void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
  {
  }
}