这是我的ViewModel中的代码
public ICommand GoToCartCommand{
get{
return new MvxCommand (() => {
var cartViewModel = Mvx.Resolve<CartViewModel>();
if (cartViewModel != null)
{
cartViewModel.cartlist.Add(new CartItemViewModel() { Name = Name, ID = ID, UnitPrice = double.Parse(Price), Quantity = 1, Description = Description, Image = Image });
}
cartListCount = cartViewModel.cartlist.Count;
var messanger = Mvx.Resolve<IMvxMessenger>();
var message = new MyMessage(this,"Product has been added",cartListCount);
messanger.Publish(message);
});
}
}
public class MyMessage : MvxMessage
{
public int CartList_Count{ get; set;}
public string Message{ get; set;}
public MyMessage(object sender,string _message,int _cartlist_count) : base(sender) {
this.Message = _message;
this.CartList_Count = _cartlist_count;
}
}
这是我的视图中的代码:
var messenger = Mvx.Resolve<IMvxMessenger>();
_token = messenger.Subscribe<MyMessage>(OnInputIsNeeded,MvxReference.Strong);
private void OnInputIsNeeded(MyMessage _Message)
{
Toast.MakeText (this.Activity,_Message.Message, ToastLength.Short).Show();
}
我的问题是,当我转到下一个视图OnInputisNeeded
后回到此视图时,该方法被调用两次。
当从这个视图返回时,当我再次访问此视图并单击按钮时,我得到NullReferenceException
。
解决方案是什么?
这是什么意思: 没有注册MvxSubscriberChangeMessage类型的消息
如何注册MvxSubscriberChangeMessage
?