0

C#ファイル:

public partial class MainWindow : Window
{
    public DelegateCommand<ICollection<string>> TestCommand { get; set; }

    public ICollection<string> TestParameter
    {
        get
        {
            List<string> lstParams = new List<string>() { "test", "test2", "test3" };
            return lstParams;
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        TestCommand = new DelegateCommand<ICollection<string>>(TestMethod);
    }

    private void TestMethod(ICollection<string> param)
    {
        if (param != null)
        {
            lblTest.Content = "Hit";
        }
    }
}

.XAMLファイル

<Window x:Class="WPFAttachedBehaviorTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WPFAttachedBehaviorTest"
    Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction CommandParameter="{Binding Path=TestParameter}" Command="{Binding Path=TestCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
<Label x:Name="lblTest"></Label>
</Window>

TestParameterゲッターのブレークポイントは起動しますが、TestMethodは起動しません。

[出力]ウィンドウにバインディングエラーは表示されません(逆に、TestCommand2のスペルを意図的に間違えると、文句を言うので、これは正しいと思います)

これは、PrismDelegateCommandとExpressionBlendInvokeCommandActionの動作を使用しています

4

2 に答える 2

2

問題が見つかりました...それはシーケンスの問題でした-InitializeComponent()の後にコマンドを割り当てて、XAMLを処理させました(したがって、バインディング式を最初に評価します-この時点では、TestCommandプロパティはまだNULLです)

私の愚かな初心者の間違い。

于 2012-11-06T03:33:26.363 に答える
1

コマンドのメソッドで、逆変の方法で型にパラメーターをキャストしようとしていICommandたことが原因で、私自身の実装で同様の問題が発生しました。Prismがどのように見えるかはわかりませんが、コードをデバッグして調べることをお勧めします。それ以外の場合、コードにエラーは表示されません。Execute()TDelegateCommand<T>

于 2012-11-06T03:03:37.297 に答える