从类函数获取回调

时间:2019-10-21 07:55:18

标签: c# class delegates

我试图弄清楚如何等待类函数的回调(我想)。

我的课看起来像这样:

public class DataLogOut
{
    public delegate void OnLogoutResponse(ResponseData data);
    public event OnLogoutResponse onLogoutResponse;

    public static void LogoutPlayer()
    {

        new EndSessionRequest().SetDurable(true).Send((response) => {
            if (!response.HasErrors)
            {

                GS.Reset();

                if (onLogoutResponse != null)
                {
                    onLogoutResponse(new ResponseData()
                    {
                        //data = response
                    });
                }

            }
            else
            {

                if (onLogoutResponse != null)
                {
                    onLogoutResponse(new ResponseData()
                    {
                        errors = response.Errors,
                        hasErrors = response.HasErrors
                    });
                }

            }
        });

    }
}

现在我这样称呼“ LogOutPlayer”:

public void LogOut()
    {
        DataLogOut.LogoutPlayer();
        DataLogOut.onLogoutResponse += onLogoutResponse;
    }

现在我在两个脚本中都遇到错误。 在第一篇中,我得到了:委托调用可以简化

...,并且在第二个脚本中,我不允许这样做:

DataLogOut.onLogoutResponse += onLogoutResponse;

真的希望有人可以帮助我,在此先感谢:-)

1 个答案:

答案 0 :(得分:2)

您的代码中有几个问题。请查看评论:

public class DataLogOut
{
    // No need for this, we will use "EventHandler"
    // public delegate void OnLogoutResponse(ResponseData data);

    //public event OnLogoutResponse onLogoutResponse; -> replaced by
    public event EventHandler<ResponseData> onLogoutResponse;

    // Convenience Method to fire the event
    protected virtual void OnLogoutResponse( ResponseData data )
    {
         var handler = onLogoutResponse;
         if( handler != null ){
            handler( this, data );
         }
    }

    // Let's simplify it by making it non-static
    //public static void LogoutPlayer()
    public void LogoutPlayer
    {

        new EndSessionRequest().SetDurable(true).Send((response) => {
            if (!response.HasErrors)
            {

                GS.Reset();

                OnLogoutResponse(new ResponseData()
                {
                   //data = response
                });
            }
            else
            {
                OnLogoutResponse(new ResponseData()
                {
                    errors = response.Errors,
                    hasErrors = response.HasErrors
                });
            }
        });

    }
}

用法:

public void LogOut()
{
    // I made it non-static, so we need an instance ...
    var logout = new DataLogout();
    // first register for the event, then have it fired.
    logout.onLogoutResponse += onLogoutResponse;
    // ^-- You tried to register the handler on the class. Which failed, 
    //     because the event was not static.
    logout.LogoutPlayer();
}

// the handler's signature now must look like this:
public void onLogoutResponse( object sender, ResponseData data ){
   // your code here
}

如果您想保留它static,也可以将该事件设为静态:

public static event EventHandler<ResponseData> onLogoutResponse;

然后,您还需要使便捷事件触发为静态

protected static void OnLogoutResponse( ResponseData data )
    {
         var handler = onLogoutResponse;
         if( handler != null ){
            handler( typeof(DataLogout), data ); // cannot use "this", of course in static context.
         }
    }

然后可以按照您的示例使用它:

public void LogOut()
{
    // first register for the event, then have it fired.
    DataLogout.onLogoutResponse += onLogoutResponse;
    // ^-- You tried to register the handler on the class. Which failed, 
    //     because the event was not static.
    DataLogout.LogoutPlayer();
}

// the handler's signature now must look like this:
public void onLogoutResponse( object sender, ResponseData data ){
   // your code here
}