带有快捷方式和自定义 RoutedCommand 的 WPF 菜单

发布于 2024-09-05 17:09:33 字数 902 浏览 10 评论 0原文

我是 WPF 新手,所以我可能会遗漏一些东西。我的 MainWindow 类中有一个简单的函数,称为 StartService。我想向我的应用程序添加一个带有快捷键 Ctrl+S 的菜单项“启动服务”。我必须执行以下操作:

  1. 在我的 MainWindow 类中,我必须定义:

    public static RoutedCommand StartServiceRoulatedCmd = 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 StopServiceRoulatedCmd 等等?

Im new to WPF so i might be missing something. I have a simple function in my MainWindow class called StartService. I wanted to add a menu item "Start Service" with a shortcut Ctrl+S to my application. I had to do the following:

  1. In my MainWindow class i had to define:

    public static RoutedCommand StartServiceRoutedCmd = new RoutedCommand();

  2. In my XAML code i added:

<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>

Things are working. I'm wondering if this is the correct and organized way to go? I am gonna need a shortcut for my StopService function. Does this mean i will need to define a new RoutedCommand StopServiceRoutedCmd, and so on for every shortcut i need?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-09-12 17:09:33
<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>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文