C# 方法在ViewModel-MVVMCross中未被调用

C# 方法在ViewModel-MVVMCross中未被调用,c#,xamarin,mvvmcross,C#,Xamarin,Mvvmcross,我有两个视图:MainView和ProfileView 用户在ProfileView中设置Age属性,并单击上一个箭头按钮(PreviousDialog bind),以便转到MainView通过消息传递协议更新Age属性 当用户单击上一个箭头按钮时,ProfileViewModel中的以下实现不会调用NotifyUpdate方法。我想知道我错过了什么或做错了什么 ProfileViewModel.cs public ICommand PreviousDialog { get {

我有两个视图:
MainView
ProfileView

用户在
ProfileView
中设置
Age
属性,并单击上一个箭头按钮(PreviousDialog bind),以便转到
MainView
通过
消息传递
协议更新
Age
属性

当用户单击上一个箭头按钮时,
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);
}
public ICommand previous对话框
{
得到
{
NotifyUpdate();
返回新的MvxCommand(()=>ShowViewModel());
}
}
//未调用以下方法
私有void NotifyUpdate()
{
var消息=新的CustomMessage(此,年龄);
var messenger=Mvx.Resolve();
发布(消息);
}

当您按下
上一个箭头按钮时,
ICommand
属性的
getter
。取而代之的是调用
ICommand
的方法,该方法调用您提供给
MvxCommand
委托

如果希望在单击
上一个箭头按钮时调用
NotifyUpdate
,则应将
NotifyUpdate
调用与
ShowViewModel()
一起放入一个单独的方法中,并将该方法传递到
MvxCommand
…如下所示:

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

private void NotifyAndNavigate()
{
    NotifyUpdate();
    ShowViewModel<MainViewModel>();
}
public ICommand previous对话框
{
得到
{
返回新的MvxCommand(()=>NotifyAndNavigate());
}
}
私有void NotifyAndNavigate()
{
NotifyUpdate();
ShowViewModel();
}

b基本正确,但您不需要为其创建单独的方法。只需使用新的MvxCommand(()=>{NotifyUpdate();ShowViewModel();});