В вашем коде есть несколько проблем. Пожалуйста, смотрите комментарии:
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
}