c# - How to call another view models constructor in MVVM Light -


i've got method authenticates users login. if user correctly logs in i'd call 2 view models contructors in case user has logged in. below sample code. best way acheive this?

sending view model:

        if (username == username && password == password)         {             projectmanager.instance.isadmin = true;             isloggedin = true;             isloggedin = true;             validloginimage();             loginstatus = "admin logged in";             messengerinstance.send(true);          } 

receiving view model:

    private void registerformessages()     {         messengerinstance.register<bool>(this, updateenabled);     }      private void updateenabled(bool b)     {         isloggedin = b;     } 

the registerformessage method never gets called.

since you're using mvvmlight, i'd send message containing boolean calls method in view model update it. register messages in view model needs updating. wait "ok i'm logged in" boolean, run method updates isenabled property. put in view model needs updated:

/// <summary>         /// listen messages other viewmodels         /// </summary>         private void registerformessages()         {             messengerinstance.register<bool>(this, updatemystuff);         }  private void updatemystuff(bool b)         {             isenabled=b;         } 

then send message when user logs in this:

//logged in code here...

messengerinstance.send(true);  //or isloggedin property 

you can send false if view has logout or whatever.

this let view model know status of whether user logged in, , allow update accordingly. obviously, need make sure implement inotifypropertychanged etc. part of mvvmlight already. if viewmodel implements mvvmlight's viewmodelbase, ready you.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -