【问题标题】:What parameter does CommandBehaviorBase.ExcuteCommand() take in PRISM 6CommandBehaviorBase.ExcuteCommand() 在 PRISM 6 中采用什么参数
【发布时间】: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;
        }
    }
}

感谢您对此的任何帮助。

问候 格特

【问题讨论】:

    标签: c# wpf prism


    【解决方案1】:

    直观地说,“要执行的命令的参数”。

    看代码,好像是这样的(第121行):

    https://github.com/PrismLibrary/Prism/blob/75206c591ce547d7e78787f1ba52cb97257052a5/Source/Wpf/Prism.Wpf/Interactivity/CommandBehaviorBase.cs

    如果您使用CommandParameter,只需传递null,因为无论如何它都会被忽略。

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多