2016-07-07 54 views
1

我有两个视图:MainViewProfileView方法不会在ViewModel中调用 - MVVMCross

用户设置Age属性在ProfileView和以去MainView经由Messaging协议来更新Age属性点击先前箭头按钮(PreviousDialog绑定)。

当用户单击上一个箭头按钮时,ProfileViewModel中的以下实现不会调用NotifyUpdate方法。我想知道我错过了什么或者错了什么?

ProfileViewModel.cs

public ICommand PreviousDialog 
{ 
    get 
    { 
     NotifyUpdate(); 
     return new MvxCommand(() => ShowViewModel<MainViewModel>()); 
    } 
} 

// the following method does not get called 
private void NotifyUpdate() 
{ 
    var message = new CustomMessage(this, Age); 
    var messenger = Mvx.Resolve<IMvxMessenger>(); 
    messenger.Publish(message); 
} 

回答

3

当您按下previous arrow buttonICommand财产getter不会被调用。取而代之的是Execute方法ICommand被调用,其调用delegate您提供给MvxCommand ... ShowViewModel<MainViewModel>()

如果你想NotifyUpdate当点击previous arrow button时被调用,你应该把NotifyUpdate呼叫到一个单独的方法以及ShowViewModel<MainViewModel>()和传递方法进入MvxCommand ....这样的事情:

public ICommand PreviousDialog 
{ 
    get 
    { 
     return new MvxCommand(() => NotifyAndNavigate()); 
    } 
} 

private void NotifyAndNavigate() 
{ 
    NotifyUpdate(); 
    ShowViewModel<MainViewModel>(); 
} 
+2

Baiscally没错,你不需要为它做一个单独的方法。只需使用新的MvxCommand(()=> {NotifyUpdate(); ShowViewModel ();}); – Cyriac