简体   繁体   English

C#、. net,委托,异步回调。 我在这里做错了什么?

[英]c#, .net, delegate, asynchronous callback. What am I doing wrong here?

Heres a little class for a button in a menu in a game. 这是游戏菜单中按钮的一个小类。

I'd like to be able to pass a delegate method _triggerMethod when I'm instantiating each button. 我希望能够在实例化每个按钮时传递委托方法_triggerMethod。 Then that delegate method will get called when that button instances trigger method is called. 然后,当该按钮实例触发方法被调用时,该委托方法将被调用。

I'm trying out delegates in C# for the first time here. 我在这里第一次尝试使用C#进行委托。 And as far as I'm interpreting the documentation here what I'm doing should work but visual studio is giving me a compile error in the Trigger method. 就我在这里解释文档而言,我正在做的事情应该可以工作,但是Visual Studio在Trigger方法中给了我一个编译错误。

According to msdn article the code calling the delegate doesnt need to know about the original methods paramaters and such. 根据msdn的文章,调用委托的代码不需要了解参数等的原始方法。 What am I doing wrong? 我究竟做错了什么? Also in the msdn article they are only typing "Del" and that does not work for me. 同样在msdn文章中,他们只键入“ Del”,这对我不起作用。 I must type "Delegate" which is odd. 我必须输入“ Delegate”,这很奇怪。

class MenuItem
    {
        private Rectangle clickArea;
        private string displayText;
        private Vector2 _position;
        private Delegate _triggerMethod;

        public MenuItem(Vector2 pos,string txt,Delegate trig)
        {
            displayText = txt;
            _position = pos;
            _triggerMethod = trig;
        }

        public void Draw(SpriteBatch sb)
        {
        }

        public void Select()
        {
        }

        public void DeSelect()
        {
        }

        public void IsMouseOnMe()
        {
        }

        public void Trigger()
        {
            _triggerMethod();
        }
    }

You haven't created any delegate definition. 您尚未创建任何委托定义。

Example (from the MSDN-page you linked): 示例(从您链接的MSDN页面):

public delegate void Del(string message);

Then, you need to use that as your Type: 然后,您需要使用它作为您的类型:

    ** snip **
    private Del _triggerMethod;

    public MenuItem(Vector2 pos,string txt,Del trig)
    {
        displayText = txt;
        _position = pos;
        _triggerMethod = trig;
    }
    ** snip **

    public void Trigger()
    {
        _triggerMethod("some message");
    }

You can pass references of the delegate around without actually knowing what arguments it expects (since it's just a normal reference), but when you want to invoke it, you do need to give it the correct parameters. 您可以传递委托的引用而无需实际知道期望的参数(因为它只是一个普通引用),但是当您要调用它时,确实需要为其提供正确的参数。

You need to define the Del type if you want to use it: 如果要使用Del类型,则需要定义它:

public delegate void Del(string message);

public class MenuItem
{
    private Del _triggerMethod;
    public void Trigger()
    {
        _triggerMethod("Message");
    }
}

Note that you can use the built-in Action<string> delegate type instead of defining your own: 请注意,您可以使用内置的Action<string>委托类型,而不用定义自己的委托类型:

Action<string> _triggerMethod;
_triggerMethod("Message");

If you just use the Delegate type, you can invoke it using DynamicInvoke : 如果仅使用Delegate类型,则可以使用DynamicInvoke调用它:

public void Trigger()
{
    _triggerMethod.DynamicInvoke();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM