引发事件 c# 给出错误:没有重载匹配委托 EventHandler

Fut*_*ake 1 c# events delegates unity-game-engine

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

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 );
        }
    }  
}
Run Code Online (Sandbox Code Playgroud)

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

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

“OnBaseObjectChange”没有重载匹配委托“EventHandler”

我以前从未处理过事件,但我真的不明白如何解决这个问题。遗憾的是,我也不太了解微软关于事件的文档:(

如果需要额外说明,请告诉我!
所有的帮助都非常感谢!

ali*_*sce 5

只需改变这种方法。因为您的活动还需要EventArgs

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

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

  handler(this, e);
Run Code Online (Sandbox Code Playgroud)

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

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

void OnBaseObjectChange(System.Object sender)
{
     Debug.Log("event is raised by element");
}
Run Code Online (Sandbox Code Playgroud)

你可以这样称呼它:

handler(this);
Run Code Online (Sandbox Code Playgroud)