c# - Passing parameters from Detail to MainViewModel in MVVMCross -
i have 2 views : mainview
, detailview
. have list of items display , when user select item , passing item properties detailviewmodel
, user able update these values.
everything works far, wonder how passing updated values mainviewmodel
?
mainviewmodel.cs
public mainviewmodel selecteditem { { 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 ; } }
one way pass variables between viewmodels
messenger
based solution.
mvvmcross messenger can found in 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 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; } }
take @ http://slodge.blogspot.nl/2013/05/n9-getting-message-n1-days-of-mvvmcross.html full guide mvvmcross messenges
.
edit using age , category in message
public selecteditemmessage(object sender, double age, int category) : base(sender) { age = age; category = category; } public double age { get; set; } public int category{ get; set; } }
changing mainviewmodel onmessagereceived method
private void onmessagereceived(selecteditemmessage obj) { age = obj.age; category= obj.category; }
Comments
Post a Comment