如何在方法内返回Action <bool>?</bool>

时间:2013-03-18 12:45:40

标签: c# callback return action

我的课程中有以下代码:

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

我也希望从我的应用程序中调用此返回值。这些代码在我的库中。我该怎么办?

1 个答案:

答案 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);