In the previous blog post my first mvvm demo, debuglzq implemented a simple demo using mvvm. Next we will use the prism framework to implement it again to feel the convenience brought by Prism! For more information about PRISM, see the previous debuglzq blog prepare for prism.
Create a new project and add corresponding folders to the project. Because this project only has one view in mainwindow, no views folder or model is added, so there is no modes folder. We only add one viewmodels folder and add a mainwindowviewmodel class to it.
To use the delegatecommand and icationicationobject provided by the prism framework, you need to add a reference to prism. refer to the previous blog post.
The mainwindowviewmodel class is implemented as follows:
Using System; Using Microsoft. Practices. Prism. viewmodel; // Using Microsoft. Practices. Prism. commands; // Namespace Mvvmpro1prism. viewmodels { Class Mainwindowviewmodel: icationicationobject { // Mainwindow has two inputs and one output, corresponding to three "data attributes" Private String Input1; Public String Input1 { Get { Return Input1 ;} Set {Input1 = Value; raisepropertychanged ( " Input1 " );}} Private String Input2; Public String Input2 { Get { Return Input2 ;} Set {Input2 = Value; raisepropertychanged ( " Input2 " );}} Private String Output1; Public String Output1 { Get { Return Output1 ;} Set {Output1 = Value; raisepropertychanged ( " Output1 " );}} // One button, a total of "command attributes" Public Delegatecommand addcommand { Get ; Set ;} Private Void Add () {output1 = Input1 + Input2 ;} // Ctor Public Mainwindowviewmodel () {addcommand = New Delegatecommand ( New Action (ADD ));}}}
Pay attention to the methods provided by the prism framework !! (Key Point !)
Add a binding in the view (same as the previous one ):
<Window X: class = " Mvvmpro1prism. mainwindow " Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation " Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml " Title = " Mainwindow " Height = " 350 " Width = " 525 " > <Grid. rowdefinitions> <rowdefinition Height = " 40 " /> <Rowdefinition Height = " 40 " /> <Rowdefinition Height = " 40 " /> <Rowdefinition Height = " * " /> </Grid. rowdefinitions> <textbox grid. Row = " 0 " Fontsize = " 26 " TEXT =" {Binding input1} " /> <Textbox grid. Row = " 1 " Fontsize = " 26 " TEXT = " {Binding input2} " /> <Textbox grid. Row = " 2 " Fontsize =" 26 " TEXT = " {Binding output1} " /> <Button grid. Row = " 3 " Content = " Add " Command = " {Binding addcommand} " /> </GRID> </WINDOW>
Using System. windows; Using Mvvmpro1prism. viewmodels; Namespace Mvvmpro1prism { /// <Summary> /// Interaction logic of mainwindow. XAML /// </Summary> Public Partial Class Mainwindow: window { Public Mainwindow () {initializecomponent (); // This . Datacontext = New Mainwindowviewmodel ();}}}
OK. Run is finished:
ProgramNormal operation, the prism framework does provide convenience for the mvvm mode! What we need to do is follow the method specifications provided by the prism framework to develop the WPF mvvm program correctly and quickly.
-------------------
This article uses the delegatecommand and icationicationobject provided by Prism to simplify mvvm writing.
Prism provides the following Hello World demo (yellow rectangle:
The directory structure is as follows:
Download