1

私はWPFを初めて使用するので、何かが足りない可能性があります。MainWindowクラスにStartServiceという単純な関数があります。ショートカットCtrl+Sを使用してメニュー項目「サービスの開始」をアプリケーションに追加したいと思いました。私は次のことをしなければなりませんでした:

  1. 私のMainWindowクラスでは、以下を定義する必要がありました。

    public static RoutedCommand StartServiceRoutedCmd = new RoutedCommand();

  2. 私のXAMLコードに次を追加しました:

<MenuItem Header="_Start Service" InputGestureText="Ctrl+S" Click="OnStartService" />

<Window.CommandBindings>
    <CommandBinding Command="{x:Static loc:MainWindow.StartServiceRoutedCmd}" 
                    Executed="OnStartService" />
</Window.CommandBindings>

<Window.InputBindings>
    <KeyBinding Command="loc:MainWindow.StartServiceRoutedCmd" Gesture="CTRL+S" />
</Window.InputBindings>

物事は機能しています。これが正しい、組織化された方法であるかどうか疑問に思いますか?StopService関数のショートカットが必要になります。これは、必要なすべてのショートカットに対して、新しいRoutedCommand StopServiceRoutedCmdなどを定義する必要があることを意味しますか?

4

1 に答える 1

2
<MenuItem Header="_Start Service" InputGestureText="Ctrl+S" Command="loc:MainWindow.StartServiceRoutedCmd />

<Window.CommandBindings>
       <CommandBinding Command="{x:Static loc:MainWindow.StartServiceRoutedCmd}" 
                Executed="OnStartService" />
       </Window.CommandBindings>

<Window.InputBindings>
       <KeyBinding Command="loc:MainWindow.StartServiceRoutedCmd" Gesture="CTRL+S" />
</Window.InputBindings>
于 2010-10-17T08:59:38.100 に答える