TriggerAction拡張------ExInvokeCommandAction
7894 ワード
Wp&Win 8でコマンドバインディングを使用する場合、Buttonコントロールにコマンドバインディングが付属しているほか、InteractivityライブラリのInvokeCommandActionで実現されています(Win 8はサードパーティのNuGetパッケージを追加でインストールして使用する必要があります.私のMVFMサンプルブログにはこのライブラリがあります).しかし、使用中にInvokeCommandActionが私たちの要求を満たすことができません.主に以下の点があります.
1送信者を取得できません.
2 EventTriggerでトリガする場合にEventArgパラメータが必要になることが多いが
InvokeCommandActionは取得できません.
3複数のパラメータを渡す必要があり、満たされない場合があります.
そして私は
InvokeCommandActionでは、まずパラメータの構造体を定義するためにいくつかの改良が行われています.
次に、処理のTriggerActionを定義します.
使用時はInvokeCommandActionと同じです.
1送信者を取得できません.
2 EventTriggerでトリガする場合にEventArgパラメータが必要になることが多いが
InvokeCommandActionは取得できません.
3複数のパラメータを渡す必要があり、満たされない場合があります.
そして私は
InvokeCommandActionでは、まずパラメータの構造体を定義するためにいくつかの改良が行われています.
/// <summary>
/// CommandParameter, CommandParameter
/// </summary>
public class ExCommandParameter
{
/// <summary>
///
/// </summary>
public object Sender { get; set; }
/// <summary>
///
/// </summary>
public object EventArgs { get; set; }
/// <summary>
///
/// </summary>
public object Parameter { get; set; }
/// <summary>
///
/// </summary>
public object Parameter2 { get; set; }
/// <summary>
///
/// </summary>
public object Parameter3 { get; set; }
}
次に、処理のTriggerActionを定義します.
/// <summary>
/// InvokeCommandAction
/// </summary>
public class ExInvokeCommandAction : CustomTriggerActionBase
{
private string commandName;
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExInvokeCommandAction), null);
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExInvokeCommandAction), null);
public static readonly DependencyProperty CommandParameter2Property = DependencyProperty.Register("CommandParameter2", typeof(object), typeof(ExInvokeCommandAction), null);
public static readonly DependencyProperty CommandParameter3Property = DependencyProperty.Register("CommandParameter3", typeof(object), typeof(ExInvokeCommandAction), null);
/// <summary>
/// 。
/// </summary>
/// <value> 。</value>
/// <remarks> Command , 。</remarks>
public string CommandName
{
get
{
return this.commandName;
}
set
{
if (this.commandName != value)
{
this.commandName = value;
}
}
}
/// <summary>
/// 。 。
/// </summary>
/// <value> 。</value>
/// <remarks> CommandName , 。</remarks>
public ICommand Command
{
get
{
return (ICommand)base.GetValue(ExInvokeCommandAction.CommandProperty);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandProperty, value);
}
}
/// <summary>
/// 。 。
/// </summary>
/// <value> 。</value>
/// <remarks> ICommand.CanExecute ICommand.Execute 。</remarks>
public object CommandParameter
{
get
{
return base.GetValue(ExInvokeCommandAction.CommandParameterProperty);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandParameterProperty, value);
}
}
public object CommandParameter2
{
get
{
return base.GetValue(ExInvokeCommandAction.CommandParameter2Property);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandParameter2Property, value);
}
}
public object CommandParameter3
{
get
{
return base.GetValue(ExInvokeCommandAction.CommandParameter3Property);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandParameter3Property, value);
}
}
/// <summary>
/// 。
/// </summary>
/// <param name="parameter"> 。 , 。</param>
protected override void Invoke(object parameter)
{
ICommand command = this.ResolveCommand();
ExCommandParameter exParameter = new ExCommandParameter
{
Sender = this.AssociatedObject,
Parameter = GetValue(CommandParameterProperty),
Parameter2 = GetValue(CommandParameter2Property),
Parameter3 = GetValue(CommandParameter3Property),
EventArgs = parameter
};
if (command != null && command.CanExecute(exParameter))
{
command.Execute(exParameter);
}
}
//
public void TriggerCommand()
{
Invoke(null);
}
public void TriggerCommand(object CommandParameter)
{
TriggerCommand(null, CommandParameter);
}
public void TriggerCommand(object sender = null, object commandParameter = null, object commandParameter2 = null, object commandParameter3 = null)
{
this.CommandParameter = commandParameter;
this.CommandParameter2 = commandParameter2;
this.CommandParameter3 = commandParameter3;
Invoke(null);
}
protected ICommand ResolveCommand()
{
ICommand result = null;
if (this.Command != null)
{
result = this.Command;
}
else
{
foreach (PropertyInfo propertyInfo in this.AssociatedObject.GetType().GetTypeInfo().DeclaredProperties)
{
if (typeof(ICommand).GetTypeInfo().IsAssignableFrom(propertyInfo.PropertyType.GetTypeInfo()) && string.Equals(propertyInfo.Name, this.CommandName, StringComparison.Ordinal))
{
result = (ICommand)propertyInfo.GetValue((object)this.AssociatedObject, (object[])null);
}
}
}
return result;
}
}
使用時はInvokeCommandActionと同じです.
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<Behavior:ExInvokeCommandAction Command="{Binding Command,Source={StaticResource ViewModel}}" CommandParameter="1" CommandParameter2="2" CommandParameter3="3"/>
</i:EventTrigger>
</i:Interaction.Triggers>