2013-03-18 53 views
1

我有我的類下面的代碼:如何返回Action <bool>裏面的方法?

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

Regardess的'SetUserMeta'簽名是什麼,這會導致錯誤。你的'SetBuddyData'是無效的,需要匹配'SetUserMeta' – 2013-03-18 12:50:04

+0

當我將void更改爲bool時,我無法處理回調。我如何構建系統以獲得來自夥伴系統的異步返回值? – 2013-03-18 12:52:16

+0

請注意,您將'pMetaValue.Serialize()'作爲委託來傳遞,這對我來說似乎是一個錯誤。檢查你的SetBuddyData方法的內容 – 2013-03-18 13:02:44

回答

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你將處理布爾回調的數據,在SetUserMeta方法返回的callback(rValue);。它的機身例子顯示未來

public static void HandleUserLogin(bool isLogged) 
{ 
    Console.WriteLine("user is {0} logged in", isLogged ? "" : "not"); 
} 

您還可以利用方法組的語法和調用SetBuddyData方法,如:

SetBuddyData("my user token", myBundle, HandleUserLogin); 
相關問題