i'm building first wpf app using mvvm pattern.
the application begins login view contains button of login.
when click login button executes icommand in loginviewmodel gets response server if login succeded.
(i'm building chat based on wcf , wpf user credentials)
*what want achieve is: if login succeeded, switch signup view
(i know doesn't make sense, testing view switching)
so far i've been reading navigation via buttons. not goal understand.
all want verify user , then, load chat view (which dont have yet, why mentiond signup view doesnt make sense)
i have main window , xaml code contains content control withing grid in order switch views:
<grid> <contentcontrol name="mainwindowcontent" content="{binding currentview}"></contentcontrol> </grid>
the main window viewmodel mainwinowviewmodel contains viewmodelbase named currentview , icommands switch currentview different viewmodel each time:
public class mainwindowviewmodel { // simplified properties public viewmodelbase currentview { get; set; } public icommand viewlogincommand { get; } public icommand viewsignupcommand{ get; } public mainwindowviewmodel() { viewlogincommand =new mycommand(setcurrentviewtologinviewmodel); viewsignupcommand = new mycommand(setcurrentviewtosignupviewmodel); currentview = new loginviewmodel(); } private void setcurrentviewtologinviewmodel() { currentview = new loginviewmodel(); } private void setcurrentviewtosignupviewmodel() { currentview = new signupviewmodel(); } }
i assigned datacontext mainwindowviewmodel in mainwindow.cs
all correct templates on place in app.xaml file presents view each viewmodel:
<application.resources> <datatemplate datatype="{x:type local:loginviewmodel}"> <views:loginview /> </datatemplate> <datatemplate datatype="{x:type local:signupviewmodel}"> <views:signupview /> </datatemplate> </application.resources>
again, want main window present 1 view @ time without navigation view aside.
my question:
how make when login succeeded, currentview change signupviewmodel.
have missed anything? architecture correct? different?
the way see it, can happpen if somehow within loginviewmodel, after login succeeded, execute viewsignupcommand in datacontext doesn't make sense , doesn't work.
i cant see how binds together. thx ahead help!
btw, pardon english. if else (details, etc..) needed in order see big picture, please notify me well.
you changing currentview through commands fine view not know change without being notified. done implementing inotifypropertychanged
interface.
i typically derive each viewmodel class viewmodelbase. viewmodelbase implements inotifypropertychanged. see examples online such implementation.
you should end this:
public class mainwindowviewmodel:viewmodelbase { private viewmodelbase _currentview; //viewmodelbase or common class,or interface of both types of views. private viewmodelbase currentview { { return _currentview; } set { if(_currentview != value) { _currentview = value; onpropertychanged(); } } } }
if don't want bother re-usable viewmodelbase class can implement inotifypropertychanged on mainwindowviewmodel.
see http://www.c-sharpcorner.com/uploadfile/0b73e1/mvvm-model-view-viewmodel-introduction-part-3/ example.
Comments
Post a Comment