简体   繁体   English

Xamarin.forms / Android Facebook获得好友

[英]Xamarin.forms / Android Facebook get Friends

Hi everyone I'm using Xamarin.Forms to create an application. 大家好,我正在使用Xamarin.Forms创建应用程序。 I've been able to create a facebook login and retrieve user ID. 我已经能够创建一个Facebook登录名并检索用户ID。 I'm now trying to get the friendlist of the user. 我现在正在尝试获取用户的好友列表。 I understood that only user who have accepted the app can be shown, however I cant see the one who identified in my app ( I have a friend as test user) Here's my code: 我知道只能显示接受该应用程序的用户,但是我看不到在我的应用程序中标识的用户(我有一个朋友作为测试用户),这是我的代码:

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

The code is for android. 该代码适用于android。 The code is inspired (not to say copy pasted) from the Xamarin Facebook SDK. 该代码是从Xamarin Facebook SDK启发而来的(更不用说复制粘贴了)。 So it should work. 所以它应该工作。 It is working however the list supposed to display friend remain empty ( note that i do not catch any errors). 它正在工作,但是应该显示为朋友的列表仍然为空(请注意,我没有发现任何错误)。

Thanks for the answers! 感谢您的回答!

The callback in ContinueWith actually runs on a different synchronization context. ContinueWith的回调实际上在不同的同步上下文上运行。 The UI probably doesn't get updated. 用户界面可能未更新。 Try doing this: 尝试这样做:

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"]);
    }
}

Please not that this WILL block the UI thread and the application will remain unresponsive during the Get call. 请注意,这将阻止UI线程,并且应用程序在Get调用期间将保持无响应。 Something like this would be preferable: 最好是这样的:

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"]);
    }
}

Alright i just made a silly mistake, did not init the List I used to store the friends, should have checked twice. 好吧,我只是犯了一个愚蠢的错误,没有初始化我用来存储朋友的列表,应该检查两次。 My initial code is working fine with that. 我的初始代码可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM