CommandBehaviorBase.ExcuteCommand() 在 PRISM 6 中接受什么参数
What parameter does CommandBehaviorBase.ExcuteCommand() take in PRISM 6
我正在将 WPF 应用程序从使用 PRISM 4 升级到使用 PRISM 6.1。此应用程序使用以 CommandBehaviorBase 作为其基础的 class-class。它调用名为 ExecuteCommand 的方法。在一篇讨论升级到 PRISM 5 (Upgrade from 4.1 to 5) 的文章中,我看到此方法现在需要一个参数。 (CommandBehaviorBase class 已从 Commands 命名空间移至 Prism.Interactivity 命名空间。ExecuteCommand 方法现在将对象作为参数。)。我的问题是我应该将什么对象传递给该方法?
现在 class 的代码:
/// <summary>
/// Defines a behavior that executes a <see cref="ICommand"/> when the Return key is pressed inside a <see cref="TextBox"/>.
/// </summary>
/// <remarks>This behavior also supports setting a basic watermark on the <see cref="TextBox"/>.</remarks>
public class ReturnCommandBehavior : CommandBehaviorBase<TextBox>
{
/// <summary>
/// Initializes a new instance of <see cref="ReturnCommandBehavior"/>.
/// </summary>
/// <param name="textBox">The <see cref="TextBox"/> over which the <see cref="ICommand"/> will work.</param>
public ReturnCommandBehavior(TextBox textBox)
: base(textBox)
{
textBox.AcceptsReturn = false;
textBox.KeyDown += (s, e) => this.KeyPressed(e.Key);
textBox.GotFocus += (s, e) => this.GotFocus();
textBox.LostFocus += (s, e) => this.LostFocus();
}
/// <summary>
/// Gets or Sets the text which is set as water mark on the <see cref="TextBox"/>.
/// </summary>
public string DefaultTextAfterCommandExecution { get; set; }
/// <summary>
/// Executes the <see cref="ICommand"/> when <paramref name="key"/> is <see cref="Key.Enter"/>.
/// </summary>
/// <param name="key">The key pressed on the <see cref="TextBox"/>.</param>
protected void KeyPressed(Key key)
{
if (key == Key.Enter && TargetObject != null)
{
this.CommandParameter = TargetObject.Text;
ExecuteCommand();
this.ResetText();
}
}
private void GotFocus()
{
if (TargetObject != null && TargetObject.Text == this.DefaultTextAfterCommandExecution)
{
this.ResetText();
}
}
private void ResetText()
{
TargetObject.Text = string.Empty;
}
private void LostFocus()
{
if (TargetObject != null && string.IsNullOrEmpty(TargetObject.Text) && this.DefaultTextAfterCommandExecution != null)
{
TargetObject.Text = this.DefaultTextAfterCommandExecution;
}
}
}
感谢对此的任何帮助。
问候
格特
凭直觉,我会说,"the parameter of the command to execute"。
看代码好像是这样的(第121行):
如果您使用 CommandParameter
,只需传递 null
,因为它无论如何都会被忽略。
我正在将 WPF 应用程序从使用 PRISM 4 升级到使用 PRISM 6.1。此应用程序使用以 CommandBehaviorBase 作为其基础的 class-class。它调用名为 ExecuteCommand 的方法。在一篇讨论升级到 PRISM 5 (Upgrade from 4.1 to 5) 的文章中,我看到此方法现在需要一个参数。 (CommandBehaviorBase class 已从 Commands 命名空间移至 Prism.Interactivity 命名空间。ExecuteCommand 方法现在将对象作为参数。)。我的问题是我应该将什么对象传递给该方法?
现在 class 的代码:
/// <summary>
/// Defines a behavior that executes a <see cref="ICommand"/> when the Return key is pressed inside a <see cref="TextBox"/>.
/// </summary>
/// <remarks>This behavior also supports setting a basic watermark on the <see cref="TextBox"/>.</remarks>
public class ReturnCommandBehavior : CommandBehaviorBase<TextBox>
{
/// <summary>
/// Initializes a new instance of <see cref="ReturnCommandBehavior"/>.
/// </summary>
/// <param name="textBox">The <see cref="TextBox"/> over which the <see cref="ICommand"/> will work.</param>
public ReturnCommandBehavior(TextBox textBox)
: base(textBox)
{
textBox.AcceptsReturn = false;
textBox.KeyDown += (s, e) => this.KeyPressed(e.Key);
textBox.GotFocus += (s, e) => this.GotFocus();
textBox.LostFocus += (s, e) => this.LostFocus();
}
/// <summary>
/// Gets or Sets the text which is set as water mark on the <see cref="TextBox"/>.
/// </summary>
public string DefaultTextAfterCommandExecution { get; set; }
/// <summary>
/// Executes the <see cref="ICommand"/> when <paramref name="key"/> is <see cref="Key.Enter"/>.
/// </summary>
/// <param name="key">The key pressed on the <see cref="TextBox"/>.</param>
protected void KeyPressed(Key key)
{
if (key == Key.Enter && TargetObject != null)
{
this.CommandParameter = TargetObject.Text;
ExecuteCommand();
this.ResetText();
}
}
private void GotFocus()
{
if (TargetObject != null && TargetObject.Text == this.DefaultTextAfterCommandExecution)
{
this.ResetText();
}
}
private void ResetText()
{
TargetObject.Text = string.Empty;
}
private void LostFocus()
{
if (TargetObject != null && string.IsNullOrEmpty(TargetObject.Text) && this.DefaultTextAfterCommandExecution != null)
{
TargetObject.Text = this.DefaultTextAfterCommandExecution;
}
}
}
感谢对此的任何帮助。
问候 格特
凭直觉,我会说,"the parameter of the command to execute"。
看代码好像是这样的(第121行):
如果您使用 CommandParameter
,只需传递 null
,因为它无论如何都会被忽略。