【发布时间】:2016-07-01 11:39:02
【问题描述】:
我正在将 WPF 应用程序从使用 PRISM 4 升级到使用 PRISM 6.1。此应用程序使用一个以 CommandBehaviorBase 作为其基类的类。它调用一个名为 ExecuteCommand 的方法。在一篇讨论升级到 PRISM 5 (Upgrade from 4.1 to 5) 的文章中,我看到这个方法现在需要一个参数。 (CommandBehaviorBase 类已从 Commands 命名空间移至 Prism.Interactivity 命名空间。ExecuteCommand 方法现在将对象作为参数。)。我的问题是我应该将什么对象传递给该方法?
现在的类代码:
/// <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;
}
}
}
感谢您对此的任何帮助。
问候 格特
【问题讨论】: