0

SL4アプリケーションを構築するためにMVVMLightフレームワークを使用しています。私のシンプルなアプリは、主に単一のメインビュー(shellView)で構成されており、複数のUserControlに分割されています。これらはUIの便利な分離であるため、独自のViewModelはありません。

ShellViewには、複数のキーパッドボタン(カスタムユーザーコントロール)を含むキーパッド(カスタムユーザーコントロール)が含まれています。

DataContextが適切に設定され、階層内のすべてのユーザーコントロールによって使用されていることを(確認したため)確信しています。(ShellViewのDatacontextはShellViewModel、キーパッドのDataContextはShellViewModelなどです)。

ShellViewModelには、「ProcessKey」という名前のICommand(RelayCommand)があります。

キーパッドコントロールには、次のようなものがあります。

<controls:KeypadButton x:Name="testBtn" Text="Hello">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PressStandardKeyCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
</controls:KeypadButton>

KeypadButtonは、基本的にボタンを含むグリッドです。MouseLeftButtonUpイベントがキャッチされ、カスタムの「クリック」イベントが発生します。私がしていることを簡単に説明するためのコードをいくつか紹介しましょう。

public partial class KeypadButton : UserControl
{
    public delegate void KeypadButtonClickHandler(object sender, RoutedEventArgs e);
    public event KeypadButtonClickHandler Click;

public KeypadButton()
{
        // Required to initialize variables
    InitializeComponent();
}

    private void innerButton_Click(object sender, MouseButtonEventArgs e)
    {
        if (Click != null)
            Click(sender, new KeypadButtonEventArgs());
    }
}

public class KeypadButtonEventArgs : RoutedEventArgs
{
    public string test { get; set; }
}

ここで、innerButton_Clickの本体にブレークポイントを設定すると、Clickが適切にキャッチされ、RelayCommandへのポイントが含まれていることがわかります。ただし、何も起こりません: "Click(sender、new KeypadButtonEventArgs());" 実行されますが、それ以上はありません。

なぜこれがそう振る舞うのですか?RelayCommandで定義されているターゲット関数を実行するべきではありませんか?多分スコープ関連の問題ですか?

よろしくお願いします、乾杯、ジャンルカ。

4

2 に答える 2

1

他のコメントで指摘されているように、これはおそらくClickイベントがではないことに関連していRoutedEventます。

簡単なハックとして、のイベントMouseLeftButtonDownの代わりに使用できる場合があります。ClickUserControl

<!-- Kinda Hacky Click Interception -->
<controls:KeypadButton x:Name="testBtn" Text="Hello">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonDown">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PressStandardKeyCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
</controls:KeypadButton>

検討できるもう1つのオプションは、Buttonの代わりにから継承することですUserControl。Silverlight Showには、おそらくこれに関連するTextBoxからの継承に関する記事があります。

于 2011-03-10T18:59:25.447 に答える
0

ルーティング イベントは次のように定義する必要があります (ドキュメントを参照)。

public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
    "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));

// Provide CLR accessors for the event
public event RoutedEventHandler Tap
{
        add { AddHandler(TapEvent, value); } 
        remove { RemoveHandler(TapEvent, value); }
}
于 2011-03-10T10:00:34.707 に答える