MVVM for WPF (step 4) -- use Prism (2), wpfmvvm
The previous article briefly introduced how to use icationicationobject and DelegateCommand in Prism. This article only describes how to use DelegateCommand <T>.
The ICommand interface initially provided a method with parameters, which we often encounter when using is useless. Prism also helps us. Of course, Prism does not forget that we sometimes need parameter drops.
In terms of definition, we use the ICommand definition. The DelegateCommand is the same as the DelegateCommand <T>. There are differences in instantiation, and of course there are also differences in interface binding, details are as follows:
TestParameterCommand = new DelegateCommand<string>(TestParameter);
private void TestParameter(string para) { TestStr = para + "para"; }
<Button Grid.Row="2" Content="TestParameter" Command="{Binding TestParameterCommand}" CommandParameter="{Binding Text, ElementName=textBox}" />
The following code uses the Command to call [with parameters]. If no parameter is included, directly pass null.
private void Button_Click(object sender, RoutedEventArgs e) { (this.DataContext as TestViewModel).TestParameterCommand.Execute("testpara"); }
The introduction of Prism in MVVM is just like this. The main Prism is not limited to MVVM, but we will introduce Prism separately later. After all, it is not a simple task.
Project code hosting address: https://wpfmvvm.codeplex.com/