如何将 EventSetters 与窗口样式一起使用?

发布于 2024-08-21 12:55:23 字数 1201 浏览 7 评论 0原文

我设计了一个窗口来替换标准 Chrome,并且我想使用 EventSetters 处理激活和停用事件。我收到错误“...'Activated'必须是已注册的 RoutedEvent...”:

  <EventSetter Event="Activated" Handler="Window_Activated"/>

但是,这在相同的风格下工作得很好。

  <EventSetter Event="Loaded" Handler="Window_Loaded"/>

有人遇到过这个或者知道怎么回事吗?

编辑:

<Style x:Key="Window_Cartesia" TargetType="{x:Type Window}">

  <Setter Property="WindowStyle" Value="None"/>
  <Setter Property="AllowsTransparency" Value="True"/>

  <Setter Property="Background" Value="Transparent"/>

  <EventSetter Event="Loaded" Handler="Loaded"/>

  <EventSetter Event="Activated" Handler="Window_Activated"/>

  <EventSetter Event="KeyDown" Handler="KeyDown"/>

...

编辑:

这似乎涵盖了它。

在 Loaded 事件中定义:

AddHandler Win.Activated, AddressOf Activated
AddHandler Win.Deactivated, AddressOf Deactivated

因为这是样式的代码隐藏,所以我需要一个实例引用,即 Win。我不知道这是否是完成此操作的最佳方法,但是...

编辑 1:

或者,IsActive 的触发器可以在 xaml 中处理它。

<Trigger Property="IsActive" Value="True">
...
</Trigger>

I have styled a window to replace the standard Chrome and I want to handle the Activated and Deactivated events using EventSetters. I get an error "...'Activated' must be a RoutedEvent registered..." with this:

  <EventSetter Event="Activated" Handler="Window_Activated"/>

However, this works fine in the same style.

  <EventSetter Event="Loaded" Handler="Window_Loaded"/>

Anyone run across this or know what's up?

Edit:

<Style x:Key="Window_Cartesia" TargetType="{x:Type Window}">

  <Setter Property="WindowStyle" Value="None"/>
  <Setter Property="AllowsTransparency" Value="True"/>

  <Setter Property="Background" Value="Transparent"/>

  <EventSetter Event="Loaded" Handler="Loaded"/>

  <EventSetter Event="Activated" Handler="Window_Activated"/>

  <EventSetter Event="KeyDown" Handler="KeyDown"/>

...

EDIT:

This seems to cover it.

Defined in the Loaded event:

AddHandler Win.Activated, AddressOf Activated
AddHandler Win.Deactivated, AddressOf Deactivated

Because this is code behind for a style, I need an instance reference which is Win. I don't know if this is the best way to accomplish this but...

EDIT 1:

Alternatively, a trigger for IsActive to handle it in xaml.

<Trigger Property="IsActive" Value="True">
...
</Trigger>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朦胧时间 2024-08-28 12:55:23

您能粘贴完整的样式声明吗?样式的目标类型可能存在问题。 LoadedFrameworkElement 上定义,而ActivatedWindow 上定义。尝试将 TargetType={x:Type Window} 设置为样式元素中的属性。

编辑:激活不是路由事件。因此不可能按照您的风格使用它。或者,您可以在代码隐藏中订阅此事件。

Could you paste your complete style declaration? There might be an issue with the style's target type. Loaded is defined on a FrameworkElement whilst Activated is defined on a Window. Try setting TargetType={x:Type Window} as an attribute in the style element.

Edit: Activated is not a routed event. Therefore it is not possible to use it in your style. Alternatively, you could subscribe to this event in code behind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文