WPF窗口分配事件处理程序

时间:2017-11-14 14:17:39

标签: c# wpf windows rider

我试图为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">

enter image description here

窗口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">

enter image description here

请注意,使用this.PreviewKeyDown对两者都有效,但我只是好奇为什么以前的方法在两个窗口上都不能正常工作。

1 个答案:

答案 0 :(得分:2)

WindowB没有分配名称:

<Window x:Class="WindowB" ... x:Name="PlayerWindow" ...>