自定义附加事件的处理程序

发布于 2024-08-19 01:47:36 字数 1610 浏览 3 评论 0原文

我正在使用我创建的自定义附加事件,并且我正在尝试向此事件添加处理程序

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void dataGridTasks_Drop(object sender, RoutedEventArgs e)
    {

    }
}

这里是 XAML 代码

<ListView  util:DragDropHelper.Drop="dataGridTasks_Drop">

我在运行时在 InitializeComponent 处遇到此错误

“System.String”类型的对象不能 被转换为类型 'System.Windows.RoatedEventHandler'。

有人知道为什么我会收到此错误吗? 谢谢 !

这是我的事件代码

    public static readonly RoutedEvent DropEvent = EventManager.RegisterRoutedEvent(
        "Drop", RoutingStrategy.Bubble, typeof(DropEventArgs), typeof(DragDropHelper));

    public static void AddDropHandler(DependencyObject d, RoutedEventHandler handler)
    {
        UIElement uie = d as UIElement;
        if (uie != null)
        {
            uie.AddHandler(DragDropHelper.DropEvent, handler);
        }
    }

    public static void RemoveDropHandler(DependencyObject d, RoutedEventHandler handler)
    {
        UIElement uie = d as UIElement;
        if (uie != null)
        {
            uie.RemoveHandler(DragDropHelper.DropEvent, handler);
        }
    }

DropEventArgs 代码

class DropEventArgs : RoutedEventArgs
{
    public object Data { get; private set; }
    public int Index { get; private set; }

    public DropEventArgs(RoutedEvent routedEvent, object data, int index) 
        : base(routedEvent)
    {
        Data = data;
        Index = index;
    }
}

I'm using a custom Attached Event that I created and I'm trying to add a handler to this event

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void dataGridTasks_Drop(object sender, RoutedEventArgs e)
    {

    }
}

Here the XAML code

<ListView  util:DragDropHelper.Drop="dataGridTasks_Drop">

I have this error in runtime at InitializeComponent

Object of type 'System.String' cannot
be converted to type
'System.Windows.RoutedEventHandler'.

Anyone knows why i'm getting this error ?
Thanks !

Here my Event code

    public static readonly RoutedEvent DropEvent = EventManager.RegisterRoutedEvent(
        "Drop", RoutingStrategy.Bubble, typeof(DropEventArgs), typeof(DragDropHelper));

    public static void AddDropHandler(DependencyObject d, RoutedEventHandler handler)
    {
        UIElement uie = d as UIElement;
        if (uie != null)
        {
            uie.AddHandler(DragDropHelper.DropEvent, handler);
        }
    }

    public static void RemoveDropHandler(DependencyObject d, RoutedEventHandler handler)
    {
        UIElement uie = d as UIElement;
        if (uie != null)
        {
            uie.RemoveHandler(DragDropHelper.DropEvent, handler);
        }
    }

DropEventArgs code

class DropEventArgs : RoutedEventArgs
{
    public object Data { get; private set; }
    public int Index { get; private set; }

    public DropEventArgs(RoutedEvent routedEvent, object data, int index) 
        : base(routedEvent)
    {
        Data = data;
        Index = index;
    }
}

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

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

发布评论

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

评论(1

GRAY°灰色天空 2024-08-26 01:47:36

经过几个小时检查示例和我的代码后,问题确实是由于事件的事件定义造成的。(感谢 Mihir 和 Dabblernl)。

我在 RegisterRoatedEvent 方法的第三个参数中犯了一个错误,提供了事件类型而不是处理程序类型。

正确的代码如下:

    public delegate void DropEventHandler(object sender, DropEventArgs e);

    public static readonly RoutedEvent DropEvent = EventManager.RegisterRoutedEvent(
        "Drop", RoutingStrategy.Bubble, typeof(DropEventHandler), typeof(DragDropHelper));

错误消息具有误导性。

After few hours checking the samples and my code, the problem was because of event definition of the Event indeed.(Thanks Mihir and Dabblernl).

I've done a mistake in the 3rd argument of RegisterRoutedEvent method by providing the event type instead of a Handler type.

The correct code is the following :

    public delegate void DropEventHandler(object sender, DropEventArgs e);

    public static readonly RoutedEvent DropEvent = EventManager.RegisterRoutedEvent(
        "Drop", RoutingStrategy.Bubble, typeof(DropEventHandler), typeof(DragDropHelper));

The error message was misleading.

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