c# - WPF mvvm messenger destructor -


i have mainwindow thath holds <contentcontrol ... content="{binding currentviewmodel}" in mainviewmodel switching between 2 view firstview,secondview. firstview contains usercontrol contentfirstview implementing async data sending contentsecondviewmodel. data send's time delay 1000ms. main question why when clicking on 1,2,1,2,1,2 buttons speed of updating label count in contentsecondview increasing? think contentsecondviewmodel not disposing , every time when clicking on button 2, creates new object of messenger.register ...

mainmodel.xaml:

<grid>     <grid.columndefinitions>         <columndefinition></columndefinition>         <columndefinition></columndefinition>     </grid.columndefinitions>     <contentcontrol grid.column="1" content="{binding currentviewmodel}" horizontalalignment="stretch"  verticalalignment="stretch"/>     <button content="1" command="{binding firstcommand}" horizontalalignment="left" margin="10,10,0,0" verticalalignment="top" width="75"/>     <button content="2" command="{binding secondcommand}" horizontalalignment="left" margin="10,78,0,0" verticalalignment="top" width="75"/> </grid> 

mainviewmodel.cs:

public class mainviewmodel : viewmodelbase {     /// <summary>     /// initializes new instance of mainviewmodel class.     /// </summary>     ///       public relaycommand firstcommand     {                 {             return new relaycommand(() => switchfirst());         }     }      public relaycommand secondcommand     {                 {             return new relaycommand(()=>switchsecond());         }     }      public static readonly firstviewmodel firstviewmodel = new firstviewmodel();     public static readonly secondviewmodel secondviewmodel = new secondviewmodel();      viewmodelbase currentviewmodel;     public viewmodelbase currentviewmodel     {                 {             return currentviewmodel;         }         set         {             if (currentviewmodel == value)                 return;             currentviewmodel = value;             raisepropertychanged("currentviewmodel");         }     }     public void switchfirst()     {         currentviewmodel = mainviewmodel.firstviewmodel;     }     public void switchsecond()     {         currentviewmodel = mainviewmodel.secondviewmodel;     }     public mainviewmodel()     {         currentviewmodel = mainviewmodel.secondviewmodel;         ////if (isindesignmode)         ////{         ////    // code runs in blend --> create design time data.         ////}         ////else         ////{         ////    // code runs "for real"         ////}     } } 

secondview.xaml:

  <grid>     <views:contentsecondview></views:contentsecondview> </grid> 

firstview.xaml:

    <grid>     <views:contentfirstview></views:contentfirstview> </grid> 

contentfirstview.xaml:

<grid>     <label>view sending data</label> </grid> 

contentsecondview.xaml:

    <grid>     <label>view sending data</label> </grid> 

contentfirstviewmodel.cs:

public class contentfirstviewmodel {     public relaycommand sendmessage     {                 {             return new relaycommand(() => send());         }     }     public void send()     {         messenger.default.send<messagecommuniactor>(new messagecommuniactor {state=1 });     }       public void increase()     {         while(true)         {             system.threading.thread.sleep(1000);             messenger.default.send<messagecommuniactor>(new messagecommuniactor { state = 1 });         }      }       public contentfirstviewmodel()     {         action = new action(increase);         iasyncresult result = a.begininvoke(null, null);     } } 

contentsecondviewmodel.cs:

public class contentsecondviewmodel:viewmodelbase {     private int count;      public int count     {                 {             return this.count;         }          set         {             this.count = value;             this.raisepropertychanged("count");         }     }      public contentsecondviewmodel()     {         messenger.default.register<messagecommuniactor>(this, (key) =>          {              count += key.state;          });     } } 

your datatemplate ensures new view created every time contentfirstviewmodel present. instead, go singleton approach singleton view injected when needed. can use prism's regionmanager or simple binding that.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -