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();});