到目前为止一切正常,但我想知道如何将更新的值传递给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 ; }}解决方法 在viewmodels之间传递变量的一种方法是基于Messenger的解决方案.
MvvmCross Messenger可以在NuGet中找到.
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;}总结
以上是内存溢出为你收集整理的c# – 在MVVMCross中将参数从Detail传递给MainViewModel全部内容,希望文章能够帮你解决c# – 在MVVMCross中将参数从Detail传递给MainViewModel所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)