我的课程中有以下代码:
private static void SetUserMeta(string pUserToken, string pMetaKey, string pMetaValue, Action<bool> callback)
{
BuddyClient client = CreateBuddy();
bool rValue = false;
client.LoginAsync((user, state) =>
{
if (state.Exception != null)
{
rValue = false;
}
else
{
client.Metadata.SetAsync((result, resultState) =>
{
if (resultState.Exception != null)
{
rValue = false;
}
else
{
rValue = true;
}
}, key: pMetaKey, value: pMetaValue);
}
callback(rValue);
}, token: pUserToken);
}
我希望得到rValue
并从我的其他方法中返回
public static void SetBuddyData(string pUserToken, BuddyData pMetaValue, Action<bool> callback)
{
//my problem is here and I don't know how to get and return data from SetUserMeta
return SetUserMeta(pUserToken, "SavedGameData", pMetaValue.Serialize());
}
我也希望从我的应用程序中调用此返回值。这些代码在我的库中。我该怎么办?
答案 0 :(得分:0)
只需将回调传递给SetUserMeta
public static void SetBuddyData(string pUserToken, BuddyData pMetaValue, Action<bool> callback)
{
SetUserMeta(pUserToken, "SavedGameData", callback);
}
并像这样致电SetBuddyData
SetBuddyData("my user token", myBundle, isLoggedIn => HandleUserLogin(isLoggedIn));
在HandleUserLogin
处,您将处理bool回调数据,并在callback(rValue);
方法SetUserMeta
处返回。这是身体的例子
下一个
public static void HandleUserLogin(bool isLogged)
{
Console.WriteLine("user is {0} logged in", isLogged ? "" : "not");
}
您还可以利用方法组语法并调用SetBuddyData
方法,如:
SetBuddyData("my user token", myBundle, HandleUserLogin);