我希望在按下回车键时调用 KeyPress 方法,因此我将整个窗口的 KeyDown 事件写入窗口定义(如下所示):
<Window x:Name="window" x:Class="MoonLander.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MoonLander"
xmlns:oxy="http://oxyplot.org/wpf"
mc:Ignorable="d"
Loaded="OnLoaded"
KeyDown="KeyPress"
Title="Moon Lander 2018" Height="580.714" Width="958.824" AutomationProperties.AcceleratorKey="" Background="White">
我在我的 MainWindow类中定义了函数 KeyPress,如下所示:
public void KeyPress(object sender, KeyEventArgs e)
{
//Do something
}
任何想法为什么我会收到此错误消息?:
错误 CS1061 'MainWindow' 不包含 'KeyPress' 的定义并且 >> 没有可访问的扩展方法 'KeyPress' 接受类型 >>'MainWindow' 的第一个参数可以找到(您是否缺少 using 指令或 >> 程序集参考?)
我需要将焦点设置到窗口吗?(我尝试使用 Loaded="OnLoaded" 执行此操作,但有相同的错误消息)
我尝试更改保护级别并将第一个参数更改为 MainWindow 对象,但我得到了同样的错误。