从类函数获取回调

曼萨

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

我的课看起来像这样:

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;

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

菲尔多

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

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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章