c# - Prism 6: Multiple Views get data from one ViewModel -
so im working week prism , it´s great :) question now: can data 1 viewmodel multiple views (in cases 2 views). work viewmodellocator brian lagunas in webinar -> https://www.youtube.com/watch?v=zfby2nfykqy
the example webinar: have views named viewa + viewb , viewmodels named viewaviewmodel + viewbviewmodel. want locator take viewaviewmodel both viewa , viewb.
code viewa:
<usercontrol x:class="mvvm_prism_mein_einfaches_beispiel.views.viewa" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:mvvm_prism_mein_einfaches_beispiel.views" xmlns:prism="http://prismlibrary.com/" prism:viewmodellocator.autowireviewmodel="true" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <grid> <label x:name="lbl_firstname" content="firstname:" horizontalalignment="left" margin="61,38,0,0" verticalalignment="top"/> <textbox x:name="tb_firstname" horizontalalignment="left" height="23" margin="129,38,0,0" textwrapping="wrap" text="{binding firstname, updatesourcetrigger=propertychanged}" verticalalignment="top" width="120"/> <label x:name="lbl_lastname" content="lastname:" horizontalalignment="left" margin="61,70,0,0" verticalalignment="top"/> <textbox x:name="tb_lastname" horizontalalignment="left" height="23" margin="129,70,0,0" textwrapping="wrap" text="{binding lastname, updatesourcetrigger=propertychanged}" verticalalignment="top" width="120"/> <label x:name="lbl_updated" content="updated:" horizontalalignment="left" margin="61,102,0,0" verticalalignment="top"/> <textblock x:name="txb_update" horizontalalignment="left" margin="129,107,0,0" textwrapping="wrap" text="{binding lastupdated}" verticalalignment="top"/> <button x:name="btn_update" content="update" horizontalalignment="left" margin="129,141,0,0" verticalalignment="top" width="75" command="{binding updatecommand}"/> <button x:name="btn_upview" content="update + view" horizontalalignment="left" margin="129,171,0,0" verticalalignment="top" width="75" command="{binding navigatecommand}" commandparameter="viewtablet"/> </grid> </usercontrol>
viewaviewmodel:
using mvvm_prism_mein_einfaches_beispiel.events; using prism.commands; using prism.events; using prism.mvvm; using prism.regions; using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows.input; namespace mvvm_prism_mein_einfaches_beispiel.viewmodels { public class viewaviewmodel : bindablebase { private string _firstname = "brian"; public string firstname { { return _firstname; } set { setproperty(ref _firstname, value); } } private string _lastname; public string lastname { { return _lastname; } set { setproperty(ref _lastname, value); } } private datetime? _testupdate; public datetime? testupdate { { return _testupdate; } set { setproperty(ref _testupdate, value); } } public icommand updatecommand { get; set; } private ieventaggregator _eventaggregator; public viewaviewmodel(ieventaggregator eventaggregator, iregionmanager regionmanager) { _eventaggregator = eventaggregator; updatecommand = new delegatecommand(execute, canexecute).observesproperty(() => firstname).observesproperty(() => lastname); _regionmanager = regionmanager; navigatecommand = new delegatecommand<string>(navigate); } private iregionmanager _regionmanager; public delegatecommand<string> navigatecommand { get; set; } private void navigate(string view) { _regionmanager.requestnavigate("contentregion", view); } private bool canexecute() { return !string.isnullorwhitespace(firstname) && !string.isnullorwhitespace(lastname); } private void execute() { lastupdated = datetime.now; _eventaggregator.getevent<updateevent>().publish(lastupdated.tostring()); } } }
viewb should viewa.
thx help. best regards shazzar
as long you're not talking same instance, can done. have register viewmodel view in viewmodellocationprovider.
viewmodellocationprovider.register<viewb, viewaviewmodel>();
*note, specific method new feature in latest preview. otherwise, have provide factory instead.
Comments
Post a Comment