c# - Correct way to transfer SelectedItemViewModel to another page in Windows Phone 8.1 -
i'm building universal app i'm concentrating on wp8.1 part of right now. i'm using mvvmlight project.
for simplicity sake, we'll assume have 2 pages in project.
page1.xaml contains list has various items. page1.xaml binded own viewmodel i.e. page1viewmodel. each item in list represents viewmodel i.e. itemviewmodel.
when tap on item, call following code:
public relaycommand<itemviewmodel> itemtapcommand { { return this._itemtapcommand ?? (this._itemtapcommand = new relaycommand<itemviewmodel>((msg) => executeitemptapcommand(msg))); } }
when item in list tapped, call following code:
private object executeitemptapcommand(itemviewmodel selecteditemviewmodel) { page2viewmodel page2viewmodel = simpleioc.default.getinstance<itemviewmodel>(); page2viewmodel.selecteditem = selecteditemviewmodel; _navigationservice.navigate(typeof(page2), selecteditemviewmodel); return null; }
as can see i'm using ioc create instance of page2viewmodel , set selecteditem
selecteditemviewmodel.
once set, navigate page2 binded page2viewmodel.
what want know is, above ok do? i've seen plenty of examples when dealing passing object 1 page done passing id example , request information page2, why request again when of information need in selecteditemviewmodel since represents tapped item in list in page1.
if it's not correct, best way go using mvvmlight?
can provide sample? i've seen messaging i'm not sure how work if navigate page2, page2viewmodel initiated when page created, how can receive message? way have above seems initiate page2viewmodel , pag2 loads, it's re-using , bind correctly i'm not sure correct way go it.
any appreciated.
thanks.
in page2viewmodel, why not use
protected override void onnavigatedto(navigationeventargs e) { page2selecteditem = e.parameter itemviewmodel; base.onnavigatedto(e); }
it looks packing data in _navigationservice.navigate call already.
with set up, happens if change to:
private object executeitemptapcommand(itemviewmodel selecteditemviewmodel) { _navigationservice.navigate(typeof(page2), selecteditemviewmodel); return null; }
Comments
Post a Comment