【发布时间】:2016-03-06 14:59:03
【问题描述】:
xaml 文件
PreviewKeyDown="Window_KeyDown"
.cs 文件
private void Window_KeyDown(object sender, KeyEventArgs e)
{
System.Windows.Forms.MessageBox.Show(e.Key.ToString());
if (e.Key == Key.Escape)
{
this.Close();
}
}
KeyPreview 设置为 true,尽管 ESC 键不会显示消息框。 (消息框会显示其他“正常”键,例如 0-9 和 a-z)。我将如何解决这个问题或找到一种方法来触发 ESC 上的某些东西?
编辑
Win.xaml
<Window x:Class="WindowsFormsApplication5.Win"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None"
PreviewKeyDown="Window_KeyDown">
</Window>
Win.xaml.cs
public partial class Win : Window
{
public Win()
{
InitializeComponent();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
System.Windows.MessageBox.Show(e.Key.ToString());
if (e.Key == Key.Escape)
{
this.Close();
}
}
}
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Win scrn = new Win();
scrn.Show();
}
}
希望这能解决任何问题,抱歉不清楚。
【问题讨论】:
-
你为什么在 WPF 中使用 Windows 窗体
MessageBox? -
重复的问题,已经回答:stackoverflow.com/questions/7691713/…
-
我不知道,我知道的最简单的显示弹出消息的方法,这真的相关吗?
-
改用
System.Windows.MessageBox? -
PreviewKeyDown事件附加在哪里?到你的窗口?然后它应该工作;它对我有用。除非您有另一个处理程序来使用键事件并取消它。