使用MVVM导航时如何刷新ViewModel

时间:2011-02-09 18:43:14

标签: silverlight mvvm windows-phone-7 light

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

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

1 个答案:

答案 0 :(得分:6)

您可以在OnNavigatingTo事件中挂接基类Page,并在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();
   }
 }
相关问题