c# – 如何在每个WPF窗口中处理快捷键?
发布时间:2020-12-15 07:55:00 所属栏目:百科 来源:网络整理
导读:我想基于一些自定义逻辑打开帮助文件到页面.如何处理用户在我的所有窗口(主窗口和模态对话框)上按F1键? 我知道如何在一个窗口中处理F1,但这可以全局完成,所以我不必在所有窗口中添加相同的代码吗? 以下是我尝试过的测试,F1在子窗口上不起作用. Window1.xam
我想基于一些自定义逻辑打开帮助文件到页面.如何处理用户在我的所有窗口(主窗口和模态对话框)上按F1键?
我知道如何在一个窗口中处理F1,但这可以全局完成,所以我不必在所有窗口中添加相同的代码吗? 以下是我尝试过的测试,F1在子窗口上不起作用. Window1.xaml: <Window x:Class="WpfApplication2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Help" Executed="CommandBinding_Executed"/> </Window.CommandBindings> <Grid> <Button Content="Open a new window" Click="Button_Click"/> </Grid> </Window> Window1.xaml.cs: using System.Windows; using System.Windows.Input; namespace WpfApplication2 { partial class Window1 : Window { public Window1() { InitializeComponent(); } void CommandBinding_Executed(object sender,ExecutedRoutedEventArgs e) { MessageBox.Show("Help"); } void Button_Click(object sender,RoutedEventArgs e) { new Window().ShowDialog(); } } } 解决方法
我在
this页面找到了答案.
也就是说,将它放在主窗口的构造函数中,例如: CommandManager.RegisterClassCommandBinding(typeof(Window),new CommandBinding(ApplicationCommands.Help,(x,y) => MessageBox.Show("Help"))); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |