C# 从类函数获取回调
我试图弄清楚如何等待类函数的回调(我猜) 我的班级是这样的:C# 从类函数获取回调,c#,class,delegates,C#,Class,Delegates,我试图弄清楚如何等待类函数的回调(我猜) 我的班级是这样的: public class DataLogOut { public delegate void OnLogoutResponse(ResponseData data); public event OnLogoutResponse onLogoutResponse; public static void LogoutPlayer() { new EndSessionRequest().S
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
});
}
}
});
}
}
public void LogOut()
{
DataLogOut.LogoutPlayer();
DataLogOut.onLogoutResponse += onLogoutResponse;
}
现在我这样称呼“LogOutPlayer”:
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
});
}
}
});
}
}
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 static event EventHandler<ResponseData> onLogoutResponse;
然后可以在您的示例中使用它:
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
}
我会用它。对于第二个错误,请添加更多信息。错误消息是什么?
onLogoutResponse
是一个成员变量,这就是为什么您在语句DataLogOut上出现错误。onLogoutResponse
。您还应该首先添加EventHandler,然后调用将触发事件的方法。@user1672994的目的是:LogoutPlayer
是一个静态方法,而事件onLogoutResponse
不是静态的。您需要一个DataLogOut
的实例来注册该事件的处理程序。好的,让我很困惑:-)您能举个例子吗。。。迷失在森林中:-)啊,是的,但是。。。这一条在下面给出了一条红线:logout.onLogoutResponse+=onLogoutResponse;不知道为什么?把你的鼠标放在红线上。它说什么?啊,我忘了你需要更改事件处理程序的签名。在回答中补充了这一点。还有一件事:我选择了非静态。但这并不是强制性的。您也可以将事件设置为静态,但是所有相关的内容也必须是静态的。如果你愿意的话,你也可以选择把上面的东西做成单身汉。谢谢:-)正是我想要的。很好地为我解释了它:-)