c# - WPF MVVM async loading notify -


i have 2 views in window 1 of them load data asynchronously. how can notify second view data loaded , need update data in label? make singleton callback?

mainwindow.xaml

    <grid>     <grid>         <grid.columndefinitions>             <columndefinition></columndefinition>             <columndefinition></columndefinition>         </grid.columndefinitions>         <views:firstview grid.column="0"></views:firstview>         <views:secondview grid.column="1"></views:secondview>     </grid> </grid> 

firstview.xaml

<grid>     <grid.columndefinitions>         <columndefinition></columndefinition>         <columndefinition></columndefinition>     </grid.columndefinitions>     <button grid.column="0" content="button" horizontalalignment="left" verticalalignment="top" width="75" command="{binding loaddata}"/>     <listview itemssource="{binding items}" grid.column="1">         <listview.itemtemplate>             <datatemplate>                 <grid>                     <grid.columndefinitions>                         <columndefinition></columndefinition>                         <columndefinition></columndefinition>                     </grid.columndefinitions>                     <label content="{binding name}" grid.column="0"></label>                     <label content="{binding hours}" grid.column="1"></label>                 </grid>             </datatemplate>         </listview.itemtemplate>     </listview> </grid> 

firstviewmodel:

 public class firstviewmodel : viewmodelbase {     /// <summary>     /// initializes new instance of firstviewmodel class.     /// </summary>     public observablecollection<itemstruct> items { get; set; }     public icommand loaddata { get; set; }     public firstviewmodel()     {         loaddata = new relaycommand(() => longloaddata());         items = new observablecollection<itemstruct>();         items.add(new itemstruct { name="first",hours="loading"});         items.add(new itemstruct { name = "second",hours="loading" });     }     public void longloaddata()     {         action load = new action(asyncload);         iasyncresult result = load.begininvoke(null, null);     }      private void asyncload()     {          foreach (itemstruct item in items)         {             random rnd = new random();             system.threading.thread.sleep(3000);             item.hours = rnd.next(1, 100).tostring();         }     } } 

secondview.xaml:

 <grid>     <listview itemssource="{binding items}">         <listview.itemtemplate>             <datatemplate>                 <grid>                     <grid.columndefinitions>                         <columndefinition></columndefinition>                         <columndefinition></columndefinition>                     </grid.columndefinitions>                     <label content="{binding name}" grid.column="0"></label>                     <label content="{binding hours}" grid.column="1"></label>                 </grid>             </datatemplate>         </listview.itemtemplate>     </listview> </grid> 

secondviewmodel:

    public class secondviewmodel : viewmodelbase {     public observablecollection<itemstruct> items { get; set; }     public secondviewmodel()     {         items = new observablecollection<itemstruct>();         items.add(new itemstruct { name="first",hours="loading"});         items.add(new itemstruct { name = "second",hours="loading" });     } } 

you can use mvvmlight messenger communication between vm's. inject child viewmodels.

public firstviemodel(imessenger messenger) {    this.messenger = messenger; } 

when data loaded invoke messenger(send) proper message.

this.messenger.send<somethingloadedmessage>(new somethingloadedmessage(..)); 

in reciever viewmodel handling quite simple:

this.messenger.register<somethingloadedmessage>(this, onsomethingloaded); 

using messenger practice when comes buildng loosly coupled multi-conpoment wpf apps.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -