在MVVMCross中将参数从Detail传递给MainViewModel

时间:2016-04-20 00:01:54

标签: c# xamarin mvvmcross

我有两种观点:MainViewDetailView。我有一个要显示的项目列表,当用户选择项目时,我将项目属性传递给DetailViewModel,用户可以更新这些值。

到目前为止一切正常,但我想知道如何将更新的值传回MainViewModel

MainViewModel.cs

public MainViewModel SelectedItem
{
  get { return _selectedItem; }
  set
  {
    _selectedItem = value;
    ShowViewModel<DetailViewModel>(
    new DetailViewModel.Parameter
    {
        Date = Date,
        Age = _selectedItem.Age,
        Category = _selectedItem.Category,
        Discount = _selectedItem.Discount,
      }
     );

    RaisePropertyChanged(() => SelectedItem);
  }
}

DetailViewModel.cs

public class DetailViewModel: MvxViewModel
{
    public double Age { get; set; }
    public double Category { get; set; }
    public double Discount { get; set; }

    public class Parameter
    {
        public DateTime Date { get; set; }
        public double Age { get; set; }
        public int Category{ get; set; }
        public double Discount { get; set; }
    }

    public void Init(Parameter param)
    {
        Age = param.Age;
        Category = param.Category;
        Discount = param.Discount ;
    }
}

2 个答案:

答案 0 :(得分:5)

ViewModels之间传递变量的一种方法是基于Messenger的解决方案。

可以在NuGet中找到MvvmCross Messenger。

<强> 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)
{
    SelectedItem = obj.SelectedItem;
}

<强> DetailViewModel

private readonly IMvxMessenger _messenger;

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

public void YourUpdateMethod() {
    var message = new SelectedItemMessage(this, SelectedItem); //SelectedItem assumed it is a ViewModel property.
    _messenger.Publish(message, typeof(SelectedItemMessage));
}

<强> SelectedItemMessage

public class SelectedItemMessage : MvxMessage
{
    public SelectedItemMessage(object sender, SelectedItem selectedItem) : base(sender)
    {
        SelectedItem = selectedItem;
    }

    public SelectedItem SelectedItem { get; set; }
}

请查看http://slodge.blogspot.nl/2013/05/n9-getting-message-n1-days-of-mvvmcross.html以获取MvvmCross Messenges的完整指南。

使用消息

中的年龄和类别进行编辑
public SelectedItemMessage(object sender, double age, int category) : base(sender)
    {
        Age = age;
        Category = category;
    }

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

更改MainViewModel OnMessageReceived方法

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

答案 1 :(得分:2)

为什么不在MainViewModel中创建DetailViewModel时保留对DetailViewModel的引用?然后,可以通过MainViewModel中的该引用获得DetailViewModel实例中更改的任何值。

private DetailViewModel _detailVM;

public MainViewModel SelectedItem
{
  get { return _selectedItem; }
  set
  {
    _selectedItem = value;
    _detailVM = new DetailViewModel.Parameter {
        Date = Date,
        Age = _selectedItem.Age,
        Category = _selectedItem.Category,
        Discount = _selectedItem.Discount
    };

    ShowViewModel<DetailViewModel>(_detailVM);

    RaisePropertyChanged(() => SelectedItem);
  }
}