Prism库中的InvokeCommandAction(写成prism:InvokeCommandAction)是比较重要的类,相对于Interactivity库中的InvokeCommandAction(写成i:InvokeCommandAction),比较重要的是增加了TriggerParameterPath属性,该属性可以实现将触发命令的事件对象本身或事件对象之属性传递给ViewModel中的命令对象。
TriggerParameterPath是对应于EventArgs中的属性路径,比如用于SelectionChanged事件,则对应于SelectionChangedEventArgs的属性的字符串,如果写 TriggerParameterPath="AddedItems",则指SelectionChangedEventArgs.AddedItems对象。CommandParameter和TriggerParameterPath不同,它就是直接传过去的参数,可以是简单的字符串,也可以是绑定的数据对象。
无废话,上代码:
(一)示例1:使用TriggerParameterPath例子
在ViewModel中定义命令 public DelegateCommand
public MainWindowViewModel()
{
...
// This command will be executed when the selection of the ListBox in the view changes.
SelectedCommand = new DelegateCommand
private void OnItemSelected(object[] selectedItems)
{
if (selectedItems != null && selectedItems.Count() > 0)
{
//Do Work here
}
}
如果写成TriggerParameterPath=""或者不写TriggerParameterPath属性,则表示把事件对象本身传过去作为CommandParameter,比如如果要传鼠标事件,则就写成TriggerParameterPath=""或不写TriggerParameterPath属性,然后在ViewModel的命令处理程序中
private void OnMouseDownCommandHandler(object mouseDownEventArgs)
{
if (mouseDownEventArgs != null && mouseDownEventArgs is MouseButtonEventArgs args)
{
//获取鼠标点的相对位置
Point p= args.GetPosition((IInputElement)args.Source); //这些用到Source对象,代表触发事件的界面元素
// 补充具体的工作代码
}
}
(二)示例2:使用CommandParameter例子
这个与上面的方法的区别在于,使用了InvokeCommandAction对象的CommandParameter属性传递SelectedItem对象, ViewModel中的代码如下:
public DelegateCommand
public MainWindowViewModel()
{
// This command will be executed when the selection of the ListBox in the view changes.
SelectedCommand = new DelegateCommand
private void OnItemSelected(object selectedItem)
{
if (selectedItem != null && selectedItem is string s)
{
//Do work here
}
}
看一下InvokeCommandAction 的定义,就清楚了。定义如下:
//
// 摘要:
// Trigger action that executes a command when invoked. It also maintains the Enabled
// state of the target control based on the CanExecute method of the command.
public class InvokeCommandAction : TriggerAction
{
//
// 摘要:
// Dependency property identifying if the associated element should automaticlaly
// be enabled or disabled based on the result of the Command's CanExecute
public static readonly DependencyProperty AutoEnableProperty;
//
// 摘要:
// Dependency property identifying the command to execute when invoked.
public static readonly DependencyProperty CommandProperty;
//
// 摘要:
// Dependency property identifying the command parameter to supply on command execution.
public static readonly DependencyProperty CommandParameterProperty;
//
// 摘要:
// Dependency property identifying the TriggerParameterPath to be parsed to identify
// the child property of the trigger parameter to be used as the command parameter.
//也就是说,从触发参数的TriggerParameterPath中解析出响应的对象,用作command Parameter
public static readonly DependencyProperty TriggerParameterPathProperty;
public InvokeCommandAction();
//
// 摘要:
// Gets or sets whther or not the associated element will automatically be enabled
// or disabled based on the result of the commands CanExecute
public bool AutoEnable { get; set; }
//
// 摘要:
// Gets or sets the command to execute when invoked.
public ICommand Command { get; set; }
//
// 摘要:
// Gets or sets the command parameter to supply on command execution.
public object CommandParameter { get; set; }
//
// 摘要:
// Gets or sets the TriggerParameterPath value.
public string TriggerParameterPath { get; set; }
//
// 摘要:
// Public wrapper of the Invoke method.
public void InvokeAction(object parameter);
//
// 摘要:
// Executes the command
//
// 参数:
// parameter:
// This parameter is passed to the command; the CommandParameter specified in the
// CommandParameterProperty is used for command invocation if not null.
protected override void Invoke(object parameter);
//
// 摘要:
// This method is called after the behavior is attached. It updates the command
// behavior's Command and CommandParameter properties if necessary.
protected override void OnAttached();
//
// 摘要:
// Sets the Command and CommandParameter properties to null.
protected override void OnDetaching();
}