2012-09-26 61 views
0

我嘗試使用Facebook C#SDK頁面中的Windows Phone示例解決此問題,但未成功。使用C#SDK獲取Windows Phone 7中的Facebook頁面

這裏的主要代碼:

private void GetPages() 
    { 
     var fb = new FacebookClient(_accessToken); 

     fb.GetCompleted += (o, e) => 
     { 
      if (e.Error != null) 
      { 
       Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message)); 
       return; 
      } 

      var result = (IDictionary<string, object>)e.GetResultData(); 
      // returns data and paging from Facebook 

      Dispatcher.BeginInvoke(() => 
      { 
       foreach (var item in result) 
       { 
        // Not sure if/how to use the custom classes here 
        //item has .Key and .Value 
        //.Key = data and .Value contains the key/value pais for each of the pages returned 
       } 

      }); 
     }; 

     fb.GetAsync("me/accounts"); 
    } 

//自定義類

public class FacebookPageCollection 
    { 
     [JsonProperty(PropertyName = "data")] 
     public FacebookPage[] data { get; set; } 
     [JsonProperty(PropertyName = "paging")] 
     public FacebookPagePaging paging { get; set; } 
    } 

    public class FacebookPage 
    { 
     [JsonProperty(PropertyName = "name")] 
     public string Name { get; set; } 

     [JsonProperty(PropertyName = "access_token")] 
     public string AccessToken { get; set; } 

     [JsonProperty(PropertyName = "category")] 
     public string Category { get; set; } 

     [JsonProperty(PropertyName = "id")] 
     public string Id { get; set; } 
    } 

    public class FacebookPagePaging 
    { 
     [JsonProperty(PropertyName = "previous")] 
     public Uri previous { get; set; } 

     [JsonProperty(PropertyName = "next")] 
     public Uri next { get; set; } 
    } 

這是變量 「結果」 返回: { 「數據」:[{ 「Name」:」值1" , 「的access_token」: 「值2」, 「類別」: 「值3」, 「ID」: 「值4」, 「燙髮」: 「管理」, 「EDIT_PROFILE」, 「CREATE_CONTENT」, 「MODERATE_CONTENT」,「CREATE_ADS 」, 「BASIC_ADMIN」]},{ 「名稱」: 「VALUE1」, 「的access_token」: 「VALUE2」, 「類別」: 「值3」, 「ID」: 「VALUE4」, 「燙髮」:[ 「給予」, 「EDIT_PROFILE」, 「CREATE_CONTENT」, 「MODERATE_CONTENT」,」 CREATE_ADS「,」BASIC_ADMIN「]}],」paging「:{」next「:」url「}}

我想要做的是檢索並保存每個頁面的詳細信息。

我一直在試圖弄清楚這一點,並在這裏和其他地方查看了一些其他帖子。我只是沒有足夠的經驗來弄清楚。

任何幫助表示讚賞。

謝謝。 斯里蘭卡

+0

就像我想在我的應用程序中使用CSharpSDK一樣,我堅持使用舊的代碼。可能有一天我會明白這一點。 – Sri

回答

1

這是一個技巧,瞭解如何在fb c#sdk中使用json響應。

這裏是JavaScript JSON和C#JSON之間的映射。 (注意沒有DateTime和其他複雜的.NET對象,因爲它不是在JSON.org發現JSON規範的一部分)

JsonObject => keyvalue pairs => IDictionary<string, object>/IDictinary<string, dynamic> 
JsonArray => array => IList<object>/IList<dynamic> 
string => string 
number => long/decimal 
boolean => bool 

這裏是你怎麼做實際的映射。

var result = (IDictionary<string, object>)e.GetResultData(); 
var data = (IList<object>)result["data"]; 
foreach(var act in data) { 
    var account = (IDictionary<string,object>) act; 
    var name = (string)account["name"]; 
    var accessToken = (string)account["access_token"]; 
    var id = (string)account["id"]; 

    // normalize to IList<string> permissions, so it is easier to work without casting. 
    var permissions = ((IList<object>)account["perms"]).Select(x => (string)x).ToList(); 
} 
相關問題