Reputation: 1451
Reading the following information, I am still stumped about what is meant by a Command method as opposed to a Command object. http://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx#sec10
The only examples given, and the only examples that I can find by searching show examples of the command object. Can someone post an example of a button bound to a command method or point me to an example elsewhere? Is it just a regular method of the view model class, a static method of the view model class, or something else entirely?
Upvotes: 2
Views: 1139
Reputation: 1451
Ah, I figured it out. The problem is that a section was completely removed from the Prism 5 Developers Guide that had existed within the Prism 4 document. I was baffled until I opened up the old Prism 4 PDF that I have saved locally on my computer.
Here is the example from the Prism 4 Developers Guide:
Invoking Command Methods from the View An alternative approach to implementing commands as ICommand objects is to implement them simply as methods in the view model and then to use behaviors to invoke those methods directly from the view. This can be achieved in a similar way to the invocation of commands from behaviors, as shown in the previous section. However, instead of using InvokeCommandAction, you use the CallMethodAction. The following code example calls the (parameter-less) Submit method on the underlying view model.
XAML
<Button Content="Submit" IsEnabled="{Binding CanSubmit}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:CallMethodAction TargetObject="{Binding}" Method="Submit"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
The TargetObject is bound to the underlying data context (which is the view model) by using the {Binding} expression. The Method parameter specifies the method to invoke. Note: CallMethodAction does not support parameters; if you need to pass parameters to the target method, you have to provide the values as properties on the view model, switch to using a command with an InvokeCommandAction, or write your own version of the CallMethodAction that will pass parameters.
It appears that the command object concept is preferred by many, but I was confused by the fact that the newer manual explicitly listed two options but only described one of them!
Upvotes: 6
Reputation: 1236
The command method is the method invoked by the command object when the command is executed.
The command method is a regular method in the viewmodel, that returns void and takes an object as a parameter.
Example from memory so might not compile. This would be in the viewmodel:
private DelegateCommand _doSomethingCommand;
public DoSomethingCommand {
get {
return _doSomethingCommand;
}
}
public MyViewModel() {
_doSomethingCommand = new DelegateCommand(OnDoSomething);
}
private void OnDoSomething(object o) {
// Do something here
}
In your markup
<Button Content="Click to do something" Command={Binding DoSomethingCommand}/>
When the button is clicked, the OnDoSomething method is invoked.
Upvotes: -1
Reputation: 606
I think "Command method" it's CompositeCommand.
The CompositeCommand is an implementation of ICommand so that it can be bound to invokers. CompositeCommands can be connected to several child commands; when the CompositeCommand is invoked, the child commands are also invoked.
CompositeCommands support enablement. CompositeCommands listen to the CanExecuteChanged event of each one of its connected commands. It then raises this event notifying its invoker(s). The invoker(s) reacts to this event by calling CanExecute on the CompositeCommand. The CompositeCommand then again polls all its child commands by calling CanExecute on each child command. If any call to CanExecute returns false, the CompositeCommand will return false, thus disabling the invoker(s).
(Prism 4.0 Readme Chapter 9: Communicating Between Loosely Coupled Components )
Example:
public class MyViewModel : NotificationObject
{
private readonly CompositeCommand saveAllCommand;
public ArticleViewModel(INewsFeedService newsFeedService,
IRegionManager regionManager,
IEventAggregator eventAggregator)
{
this.saveAllCommand = new CompositeCommand();
this.saveAllCommand.RegisterCommand(new SaveProductsCommand());
this.saveAllCommand.RegisterCommand(new SaveOrdersCommand());
}
public ICommand SaveAllCommand
{
get { return this.saveAllCommand; }
}
}
Upvotes: -1