带有快捷方式和自定义 RoutedCommand 的 WPF 菜单
我是 WPF 新手,所以我可能会遗漏一些东西。我的 MainWindow 类中有一个简单的函数,称为 StartService。我想向我的应用程序添加一个带有快捷键 Ctrl+S 的菜单项“启动服务”。我必须执行以下操作:
在我的 MainWindow 类中,我必须定义:
public static RoutedCommand StartServiceRoulatedCmd = new RoutedCommand();
在我的 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:
In my MainWindow class i had to define:
public static RoutedCommand StartServiceRoutedCmd = new RoutedCommand();
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)