MVVM for WPF (Step 3) -- use Prism (1), wpfmvvm
When using the WPF-MVVM development, the implementation of the notification interface, DelegateCommand is relatively less used, we are more of a third-party MVVM framework, among them, Microsoft's own team provides the Prism framework, which has many functions and I am learning little about it. I will briefly introduce the use of this framework.
Note: Prism version 4.1 is used here
Prism helps us implement the INotifyPropertyChanged interface first. RaisePropertyChanged not only implements the use of string notifications, but also Lambda expressions, which provides some convenience in code refactoring...
Secondly, Prism helps us implement the ICommand interface, which can be implemented using its DelegateCommand.
The following is a brief code:
Class TestViewModel: icationicationobject {private string teststr; // <summary> // the string to be notified /// </summary> public string TestStr {get {return teststr ;} set {teststr = value; RaisePropertyChanged () => TestStr );}} /// <summary> /// Test command // </summary> public ICommand TestCommand {get; set;} public TestViewModel () {TestCommand = new DelegateCommand (Test, canTest);} int I = 0; // <summary> // method executed by testcommand /// </summary> private void Test () {I ++; testStr = I. toString () ;}/// <summary> /// test whether the command is available /// </summary> /// <returns> </returns> private bool CanTest () {return true ;}}
DelegateCommand in Prism is used here, which is used without passing parameters. The next article describes how to use DelegateCommand <T> to create a Command that can pass parameters.
Project code hosting address: https://wpfmvvm.codeplex.com/