WPF 中的路由事件 - 使用操作委托

发布于 2024-09-15 09:42:51 字数 184 浏览 4 评论 0原文

我正在开发一个用户控件,并希望使用路由事件。我注意到提供了两个委托 - RoutedEventHandler 和 RoutedPropertyChangedEventHandler。第一个不传递任何信息,第二个获取属性更改的旧值和新值。但是,我只需要传递一条信息,因此我需要相当于一个 Action 委托。有提供什么吗?我可以使用 Action 委托吗?

I'm developing a user control, and wish to use a routed event. I notice that there are two delegates provided - RoutedEventHandler, and RoutedPropertyChangedEventHandler. The first that doesn't pass along any information, and the second that takes the old and new values of a property change. However, I need to pass just a single piece of information, so I want the equivalent of an Action delegate. Is there anything provided? Can I use an Action delegate?

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

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

发布评论

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

评论(1

你丑哭了我 2024-09-22 09:42:51

创建 RoutedEventArgs 的子类来保存附加数据,并将 EventHandler 与您的 args 类一起使用。这将可转换为 RoutedEventHandler 并且附加数据将在您的处理程序中可用。

您可以创建一个通用的 RoutedEventArgs 类来保存任何类型的单个参数,但是创建一个新类通常会使代码更易于阅读并且更易于修改以在将来包含更多参数。

public class FooEventArgs
    : RoutedEventArgs
{
    // Declare additional data to pass here
    public string Data { get; set; }
}

public class FooControl
    : UserControl
{
    public static readonly RoutedEvent FooEvent =
        EventManager.RegisterRoutedEvent("Foo", RoutingStrategy.Bubble, 
            typeof(EventHandler<FooEventArgs>), typeof(FooControl));

    public event EventHandler<FooEventArgs> Foo
    {
        add { AddHandler(FooEvent, value); }
        remove { RemoveHandler(FooEvent, value); }
    }

    protected void OnFoo()
    {
        base.RaiseEvent(new FooEventArgs()
        {
            RoutedEvent = FooEvent,
            // Supply the data here
            Data = "data",
        });
    }
}

Create a subclass of RoutedEventArgs to hold your additional data, and use EventHandler<T> with your args class. This will be convertible to RoutedEventHandler and the additional data will be available in your handlers.

You could create a generic RoutedEventArgs class that holds a single parameter of any type, but creating a new class usually makes the code easier to read and easier to modify to include more parameters in the future.

public class FooEventArgs
    : RoutedEventArgs
{
    // Declare additional data to pass here
    public string Data { get; set; }
}

public class FooControl
    : UserControl
{
    public static readonly RoutedEvent FooEvent =
        EventManager.RegisterRoutedEvent("Foo", RoutingStrategy.Bubble, 
            typeof(EventHandler<FooEventArgs>), typeof(FooControl));

    public event EventHandler<FooEventArgs> Foo
    {
        add { AddHandler(FooEvent, value); }
        remove { RemoveHandler(FooEvent, value); }
    }

    protected void OnFoo()
    {
        base.RaiseEvent(new FooEventArgs()
        {
            RoutedEvent = FooEvent,
            // Supply the data here
            Data = "data",
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文