3

使用手机上的后退按钮导航时如何刷新我的 ViewModel?

我正在使用手机上的后退按钮,但我相信它与调用 NavigationService.GoBack() 相同,它导航到堆栈上的上一页,但在我的 View 或 ViewModel 中未调用构造函数。

4

1 回答 1

6

您可以在基本 Page 类中挂钩 OnNavigatingTo 事件并调用 ViewModel 上的方法。我没有 VS,但伪代码是:

在 MyBasePAge 中:页面

public void OnNavigatingTo(object sender, eventargs e)
{
   var vm = this.DataContext as BaseViewModel;
   if(vm != null)
   {
      vm.Initialize();
   }
 }

你可以在离开页面之前做同样的事情:

public void OnNavigatingFrom(object sender, eventargs e)
{
   var vm = this.DataContext as BaseViewModel;
   if(vm != null)
   {
      vm.Save();
   }
 }
于 2011-02-09T19:19:28.900 回答