[英]Xamarin.forms / Android Facebook get Friends
大家好,我正在使用Xamarin.Forms創建應用程序。 我已經能夠創建一個Facebook登錄名並檢索用戶ID。 我現在正在嘗試獲取用戶的好友列表。 我知道只能顯示接受該應用程序的用戶,但是我看不到在我的應用程序中標識的用戶(我有一個朋友作為測試用戶),這是我的代碼:
private void getFriends() {
FacebookClient fb = new FacebookClient (App.Token);
fb.GetTaskAsync ("me/friends").ContinueWith (t => {
try {
var result = (IDictionary<string, object>)t.Result;
var data = (IList<object>)result ["data"];
foreach (IDictionary<string, object> friend in data) {
App.addFriendToList ((string)friend ["name"], (string)friend["id"]);
}
} catch {
App.addFriendToList ("No Friends Using imin", "0000");
}
});
}
該代碼適用於android。 該代碼是從Xamarin Facebook SDK啟發而來的(更不用說復制粘貼了)。 所以它應該工作。 它正在工作,但是應該顯示為朋友的列表仍然為空(請注意,我沒有發現任何錯誤)。
感謝您的回答!
ContinueWith
的回調實際上在不同的同步上下文上運行。 用戶界面可能未更新。 嘗試這樣做:
private void getFriends() {
FacebookClient fb = new FacebookClient (App.Token);
// There call for "Result" here will wait for the task to complete before returning.
var result = (IDictionary<string, object>)fb.GetTaskAsync ("me/friends").Result;
var data = (IList<object>)result ["data"];
foreach (IDictionary<string, object> friend in data) {
App.addFriendToList ((string)friend ["name"], (string)friend["id"]);
}
}
請注意,這將阻止UI線程,並且應用程序在Get調用期間將保持無響應。 最好是這樣的:
private async Task getFriends() {
FacebookClient fb = new FacebookClient (App.Token);
//Here the call to ConfigureAsync(true) will force the synchronization context back to the one before the call (the UI thread in this case)
var result = (IDictionary<string, object>)(await fb.GetTaskAsync ("me/friends").ConfigureAsync(true));
var data = (IList<object>)result ["data"];
foreach (IDictionary<string, object> friend in data) {
App.addFriendToList ((string)friend ["name"], (string)friend["id"]);
}
}
好吧,我只是犯了一個愚蠢的錯誤,沒有初始化我用來存儲朋友的列表,應該檢查兩次。 我的初始代碼可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.