【问题标题】:WPF Window assigning event handlersWPF 窗口分配事件处理程序
【发布时间】: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 在两者上都可以正常工作,但我只是好奇为什么前一种方法在两个窗口上都不能正常工作。

【问题讨论】:

  • 请提供足够的代码让我们了解PlayerWindowFormWindow 是什么。
  • 更多代码会很有用,但错误消息似乎表明在您的 PlayerWindow 上 PreviewKeyDown 和 PreviewKeyUp 事件被声明为非静态的,并且当通过类名而不是实例名引用成员时,只有静态成员才会可用。

标签: c# wpf windows rider


【解决方案1】:

WindowB 没有指定名称:

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

【讨论】:

  • @Ed 我没有注意到在一个窗口中它是 TitleName 在另一个窗口中。这可能会对处于相同情况的其他人有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
相关资源
最近更新 更多