1

我的课堂上有以下代码:

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());
}

而且我想从我的应用程序中调用这个返回值。这些代码在我的库中。我该怎么做?

4

1 回答 1

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);
于 2013-03-18T12:52:34.960 回答