在MVVMCross

时间:2016-04-20 18:16:14

标签: c# android xamarin mvvmcross

我一直在尝试使用mvvmcross中的消息协议将参数从DetailViewModel更新为MainViewModel。如果要在运行中更新属性,以下代码可以正常工作;但是,我很困惑,因为我有save按钮,允许用户在返回DetailViewModel之前确认MainViewModel中的更改。

我该如何处理这种情况?有办法解决吗?

MainViewModel

private readonly IMvxMessenger _messenger;
private readonly MvxSubscriptionToken _token;

public MainViewModel(IMvxMessenger messenger) {

    _messenger = messenger;
    _token = messenger.Subscribe<SelectedItemMessage>(OnMessageReceived);;
}

private void OnMessageReceived(SelectedItemMessage obj)
{
    Age = obj.Age;
    Category= obj.Category;
}

DetailViewModel

private readonly IMvxMessenger _messenger;

public DetailViewModel(IMvxMessenger messenger) {
    _messenger = messenger;
}

public void UpdateMethod() {
    var message = new SelectedItemMessage(this, SelectedItem); 
    _messenger.Publish(message, typeof(SelectedItemMessage));
}

SelectedItemMessage

public SelectedItemMessage(object sender, double age, int category) : base(sender)
    {
        Age = age;
        Category = category;
    }

    public double Age { get; set; }
    public int Category{ get; set; }
}

2 个答案:

答案 0 :(得分:2)

如果您的数据仅在用户执行<div class="background"> <div class="blend"></div> </div>操作时才会更新,请在用户按下时调用Save

UpdateMethod

答案 1 :(得分:2)

您可以使用 ACR MvvmCross User Dialogs库显示确认对话框,并仅在用户确认操作时致电UpdateMethod

var result = await UserDialogs.Instance.ConfirmAsync("Pick a choice", "Pick Title");
if (result)
{
    UpdateMethod();
}

使用前请确保Setup the library正确