【发布时间】:2017-11-14 14:17:38
【问题描述】:
我试图为 WPF 窗口(窗口 B)分配事件处理程序,就像在窗口 A 中分配的一样,但 IDE 显示 Cannot access non-static event PreviewKeyDown in static context。也许有人知道为什么会这样。我看不出两个窗口之间有什么区别。
窗口 A:
<Window x:Class="WindowA"
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"
mc:Ignorable="d"
Title="PreviewDetailWindow"
Height="512.929"
Width="809.152"
MinWidth="240"
Name="FormWindow"
WindowStartupLocation="CenterScreen"
Background="Gray"
UseLayoutRounding="True"
SnapsToDevicePixels="True">
窗口 B:
<Window x:Class="WindowB"
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:BasketMap"
mc:Ignorable="d"
Title="PlayerWindow"
Height="300"
Width="300"
Background="Gray">
请注意,使用 this.PreviewKeyDown 在两者上都可以正常工作,但我只是好奇为什么前一种方法在两个窗口上都不能正常工作。
【问题讨论】:
-
请提供足够的代码让我们了解
PlayerWindow和FormWindow是什么。 -
更多代码会很有用,但错误消息似乎表明在您的 PlayerWindow 上 PreviewKeyDown 和 PreviewKeyUp 事件被声明为非静态的,并且当通过类名而不是实例名引用成员时,只有静态成员才会可用。