The previous article introduced the overall architecture of the mvvm architecture. The following describes the implementation process of viewmodel and riaservice.
1. viewmodel: defines the attributes and Operations corresponding to the view, as follows:
Viewmodelbase. CS, inherited fromInotifypropertychanged. In this way, when the bound view changes, the attributes or methods corresponding to the viewmodel can be triggered.CodeAs follows:
Public class viewmodelbase: inotifypropertychanged
{
Protected void onnotifypropertychanged (string P)
{
If (propertychanged! = NULL)
{
Propertychanged (this, new propertychangedeventargs (p ));
}
}
Public bool isdesigntime
{
Get
{
Return (application. Current = NULL) | (application. Current. GetType () = typeof (Application ));
}
}
# Region inotifypropertychanged members
Public event propertychangedeventhandler propertychanged;
# Endregion
}
Productionviewmodel. CS, the viewmodel corresponding to the view, inherits from viewmodelbase, the Code is as follows:
Public class productionviewmodel: viewmodelbase
{
Private ienumerable <productiondatadto> _ listproduction; // query result
Private string _ searchtext; // query Condition
Private icommand _ querycommand; // query command
Public icommand querycommand
{
Get {return _ querycommand ;}
}
Public String searchtext
{
Get {return _ searchtext ;}
Set
{
_ Searchtext = value;
Onpolicypropertychanged ("searchtext ");
}
}
Public ienumerable <productiondatadto> listproduction
{
Get {return _ listproduction ;}
Set
{
_ Listproduction = value;
Onnotifypropertychanged ("listproduction ");
}
}
Testdomaincontext service = NULL;
Public productionviewmodel ()
{
_ Querycommand = new querycommand (this );
_ Querycommand. Execute (null );
}
Public void querydata ()
{
Service = new yoctestdomaincontext ();
_ Listproduction = service. Load (service. getproductiondataquery (). entities;
}
}
Here, we define an icommand to process the behavior involved in viewmodel. The Code is as follows:
Public partial class querycommand: icommand
{< br> private productionviewmodel _ productionviewmodel;
private string _ searchtext;
Public querycommand (productionviewmodel, string searchtext)
{< br> _ productionviewmodel = productionviewmodel;
_ searchtext = searchtext;
}< br> Public querycommand (productionviewmodel)
{< BR >_productionviewmodel = productionviewmodel;
}
Public bool canexecute (object parameter)
{
Return true;
}
Public event eventhandler canexecutechanged
{
Add {}
Remove {}
}
Public void execute (object parameter)
{
This. _ productionviewmodel. querydata ();
}
}
The above is a rough introduction to the viewmodel structure.