WPF 中的自定义附加事件

发布于 2024-07-26 09:12:53 字数 1288 浏览 7 评论 0原文

我可能在这里用错了术语,但我想我正在尝试创建一个附加事件。

在 Surface SDK 中,您可以执行以下操作:

<Grid Background="{StaticResource WindowBackground}" x:Name="Foo" s:SurfaceFrameworkElement.ContactChanged="Foo_ContactChanged"/>

我想创建一个自定义事件,可以以相同的方式在 XAML 中添加处理程序,但我遇到了麻烦。

我可以创建自定义路由事件,但 XAML 智能感知看不到它,并且如果我只是定期键入它,则不会添加事件处理程序。 这是我的事件定义:

public static class TagRectEvents
{
    public static readonly RoutedEvent TagRectEnterEvent = EventManager.RegisterRoutedEvent(
        "TagRectEnter", RoutingStrategy.Bubble, typeof( RoutedEventHandler ), typeof( TagRectEvents ) );

    public static void AddTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
    {
        UIElement element = d as UIElement;
        if ( element == null )
        {
            return;
        }
        element.AddHandler( TagRectEvents.TagRectEnterEvent, handler );
    }

    public static void RemoveTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
    {
        UIElement element = d as UIElement;
        if ( element == null )
        {
            return;
        }
        element.RemoveHandler( TagRectEvents.TagRectEnterEvent, handler );
    }
}

我是不是搞错了? 我看到的所有“附加行为”示例更多的是关于添加附加属性,然后使用设置该属性的元素执行操作。

I might be getting the terminology wrong here, but I think I'm trying to create an attached event.

In the Surface SDK, you can do things like:

<Grid Background="{StaticResource WindowBackground}" x:Name="Foo" s:SurfaceFrameworkElement.ContactChanged="Foo_ContactChanged"/>

I want to create a custom event for which a handler can be added in XAML in the same way, but I'm having trouble.

I can create a custom routed event, but the XAML intellisense doesn't see it and the event handler isn't added if I just type it in regularly. Here is my event definition:

public static class TagRectEvents
{
    public static readonly RoutedEvent TagRectEnterEvent = EventManager.RegisterRoutedEvent(
        "TagRectEnter", RoutingStrategy.Bubble, typeof( RoutedEventHandler ), typeof( TagRectEvents ) );

    public static void AddTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
    {
        UIElement element = d as UIElement;
        if ( element == null )
        {
            return;
        }
        element.AddHandler( TagRectEvents.TagRectEnterEvent, handler );
    }

    public static void RemoveTagRectEnterHandler( DependencyObject d, RoutedEventHandler handler )
    {
        UIElement element = d as UIElement;
        if ( element == null )
        {
            return;
        }
        element.RemoveHandler( TagRectEvents.TagRectEnterEvent, handler );
    }
}

Am I just going about it all wrong? All of the "attached behavior" examples I see are more about adding an attached property, and then doing things with elements that set that property.

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

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

发布评论

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

评论(2

你列表最软的妹 2024-08-02 09:12:53

您必须不映射命名空间,和/或附加诸如 local:TagRectEvents.TagRectEnterEvent 之类的事件。 您必须使用 TagRectEnter,而不是 TagRectEnterEvent

命名空间映射:

 xmlns:local="clr-namespace:WpfInfrastructure.WpfAttachedEvents"

用法:

<Button Content="Press" local:TagRectEvents.TagRectEnter="MyHandler" Margin="25,43,36,161" />

处理程序:

    public void MyHandler(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Hurray!");
    }

我使用了你的代码,它在这里工作正常。

You must either be not mapping the namespace, and/or attaching event like local:TagRectEvents.TagRectEnterEvent . You have to use TagRectEnter, and not TagRectEnterEvent.

Namespace mapping :

 xmlns:local="clr-namespace:WpfInfrastructure.WpfAttachedEvents"

Usage :

<Button Content="Press" local:TagRectEvents.TagRectEnter="MyHandler" Margin="25,43,36,161" />

Handler :

    public void MyHandler(object sender, RoutedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("Hurray!");
    }

I used your code, it works correctly here.

反目相谮 2024-08-02 09:12:53

为了使附加事件显示在 Intellisense 中,它必须位于驻留在卫星程序集(或 .dll 库)中的类中。 添加库的最简单方法是将“WPF 自定义控件库”项目添加到您的解决方案中。 使用 Wpf 控件库只是确保自动添加所有典型引用(使用 C# 类库则不会)。只要在 Themes/Generic 中删除其关联的 Style,就可以删除 CustomControl1.cs。 xaml。

In order to get the attached event to show up in Intellisense, it has to be in a class that resides in a satellite assembly -- or .dll library. The easiest way to add a library is add a "WPF Custom Control Library" project to your solution. Using a Wpf control library just ensures that all the typical References will be added automatically (which they won't with a C# class library.) You can delete the CustomControl1.cs just as long as you delete its associated Style in Themes/Generic.xaml.

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