引发事件C#给出错误:没有重载匹配委托EventHandler [英] Raise event c# gives error: No overload matches delegate EventHandler

查看:147
本文介绍了引发事件C#给出错误:没有重载匹配委托EventHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个事件,该事件在发生某些事情时会引发.因此,包含对引发事件的类的引用的其他类将得到通知.到目前为止,这是我得到的:

I am trying to make an event that gets raised when something happens. So other classes that contain a reference to the class that raised the event get notified. This is what i have got so far:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DropArea : MonoBehaviour {

    public event EventHandler ObjectChange;

    void Start () {
    }

    // Update is called once per frame
    void Update () {
    }

    void OnTriggerEnter(Collider other)
    {
        var correctType = FindParentWithComponent(other.gameObject);
        if (correctType != null)
        {
            var cc = correctType.GetComponent<Object_properties>();
            if(cc.VariableTypeSelector == AcceptedVariableType)
            {
                DetectedObjectChange(null); // here i want to raise the event
            }
        }
    }

    protected virtual void DetectedObjectChange(EventArgs e)
    {
        EventHandler handler = ObjectChange;
        if (handler != null)
        {
            handler(this, e );
        }
    }  
}

这是应该通过引发事件通知的类:

this is the class that should get notified by the raised event:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IfStatement : MonoBehaviour {

    public DropArea Base, Check, Increment;

    void Start()
    {
        Base.ObjectChange += OnBaseObjectChange; //this line gives me an error so i cant compile. 
    }

    // Update is called once per frame
    void Update () {

    }

    void OnBaseObjectChange(System.Object sender)
    {
        Debug.Log("event is raised by element");
    }
}

这是我得到的错误:

'OnBaseObjectChange'的重载不匹配委托'EventHandler'

No overload for 'OnBaseObjectChange' matches delegate 'EventHandler'

我以前从未处理过事件,但我真的不明白如何解决此问题.可悲的是,我也不太了解有关事件的Microsoft文档:(

I have never worked with events before but i cant really understand how to fix this. Sadly i also dont really understand the microsoft docs about events :(

如果需要进一步澄清,请告诉我!
真的很感谢所有帮助!

If extra clarification is needed pls let me know!
All help is really appreaciated!

推荐答案

只需更改此方法.因为您的事件还需要 EventArgs

Just change this method. Because your event also requires EventArgs

void OnBaseObjectChange(System.Object sender, EventArgs e)
{
    Debug.Log("event is raised by element");
}

代表持有方法的签名.如您所见,事件的方法类型带有两个参数.尝试将其与单个参数一起使用会导致错误.

Delegates hold signatures of methods. As you can see your event has a method type with two parameters. Trying to use it with a single parameter causes the error.

  handler(this, e);


或者,您可以将事件的类型更改为其他类型.例如,如果您不想在事件中使用 EventArgs ,则使用 Action< System.Object> 来保留处理程序方法的当前签名:


Alternatively, you can change the type of your event to something else. For example an Action<System.Object> to keep the current signature of your handler method, if you don't want to have an EventArgs in your event:

public event System.Action<System.Object> ObjectChange;

void OnBaseObjectChange(System.Object sender)
{
     Debug.Log("event is raised by element");
}

在哪里可以这样称呼:

handler(this);

这篇关于引发事件C#给出错误:没有重载匹配委托EventHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆