尽管已定义,但在 class 中找不到 WPF KeyDown 方法
WPF KeyDown method can't be found in class despite being defined
我希望在按下回车键时调用 KeyPress 方法,因此我将整个 window 的 KeyDown 事件写入 window 定义(如下所示):
<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 class 中定义了函数 KeyPress,如下所示:
public void KeyPress(object sender, KeyEventArgs e)
{
//Do something
}
知道为什么我会收到此错误消息吗? :
Error CS1061 'MainWindow' does not contain a definition for 'KeyPress' and >>no accessible extension method 'KeyPress' accepting a first argument of type >>'MainWindow' could be found (are you missing a using directive or an >>assembly reference?)
我需要将焦点设置到窗口吗? (我尝试使用 Loaded="OnLoaded" 执行此操作,但出现相同的错误消息)
我试过更改保护级别并将第一个参数更改为 MainWindow 对象,但我遇到了同样的错误。
我写了同样的代码但没有得到 error.You 应该在 xaml 处更改函数名称以尝试 again.And 按下 f12 键以自动创建函数。
我尝试从您的代码中复制问题。
但它没有任何问题。
请找到我使用的以下代码,
public void KeyPress(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Enter Key Pressed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我的猜测是您是手动编码的,而不是使用 IDE 来生成处理程序。这意味着中间代码不包含事件和您的方法的绑定。该绑定出现在一个隐藏文件中,通常命名如下,在您的例子中:
MainWindow.g.i.cs
您应该在 XAML 中完成,首先键入您希望处理的事件,在本例中为 KeyDown
,然后让 IDE 执行其工作通过使用 TAB 键自动生成处理程序。你应该以这样的方法结束:
private void MainWindow_KeyDown(Object sender, KeyEventArgs e)
{
}
请注意,自动生成的方法是 private
,而你的是 public
。这是您手工完成的第一个线索。
我希望在按下回车键时调用 KeyPress 方法,因此我将整个 window 的 KeyDown 事件写入 window 定义(如下所示):
<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 class 中定义了函数 KeyPress,如下所示:
public void KeyPress(object sender, KeyEventArgs e)
{
//Do something
}
知道为什么我会收到此错误消息吗? :
Error CS1061 'MainWindow' does not contain a definition for 'KeyPress' and >>no accessible extension method 'KeyPress' accepting a first argument of type >>'MainWindow' could be found (are you missing a using directive or an >>assembly reference?)
我需要将焦点设置到窗口吗? (我尝试使用 Loaded="OnLoaded" 执行此操作,但出现相同的错误消息)
我试过更改保护级别并将第一个参数更改为 MainWindow 对象,但我遇到了同样的错误。
我写了同样的代码但没有得到 error.You 应该在 xaml 处更改函数名称以尝试 again.And 按下 f12 键以自动创建函数。
我尝试从您的代码中复制问题。 但它没有任何问题。 请找到我使用的以下代码,
public void KeyPress(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Enter Key Pressed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我的猜测是您是手动编码的,而不是使用 IDE 来生成处理程序。这意味着中间代码不包含事件和您的方法的绑定。该绑定出现在一个隐藏文件中,通常命名如下,在您的例子中:
MainWindow.g.i.cs
您应该在 XAML 中完成,首先键入您希望处理的事件,在本例中为 KeyDown
,然后让 IDE 执行其工作通过使用 TAB 键自动生成处理程序。你应该以这样的方法结束:
private void MainWindow_KeyDown(Object sender, KeyEventArgs e)
{
}
请注意,自动生成的方法是 private
,而你的是 public
。这是您手工完成的第一个线索。