CommandBehaviorBase.ExcuteCommand()在PRISM 6中采用什么参数

时间:2016-07-01 11:39:02

标签: c# wpf prism

我正在使用PRISM 4升级WPF应用程序以使用PRISM 6.1。此应用程序使用具有CommandBehaviorBase作为其基类的类。它调用一个名为ExecuteCommand的方法。在一篇讨论升级到PRISM 5(Upgrade from 4.1 to 5)的文章中,我看到这个方法现在需要一个参数。 ( CommandBehaviorBase类已从Commands命名空间移动到Prism.Interactivity命名空间.ExccuteCommand方法现在将对象作为参数。)。我的问题是我应该将哪个对象传递给该方法?

现在的类代码:

/// <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;
        }
    }
}

感谢任何帮助。

问候 格特

1 个答案:

答案 0 :(得分:1)

直观地说,我要说,&#34;执行命令的参数&#34;。

查看代码,似乎就是这样(第121行):

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

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