我實現了一種方法來獲取已安裝應用程序的所有朋友。GraphRequest永遠不會提高完成
我按照一些帖子&教程,但我無法去在OnCompleted方法。
這裏我的代碼:
public class FacebookHelper : Java.Lang.Object, GraphRequest.IGraphJSONArrayCallback
{
//Facebook structure
struct FbUser
{
public string Id;
public string Name;
}
private List<Friend> allfriends;
public List<Friend> Friends { get; set; }
/// <summary>
/// Link a new users with his friends
/// </summary>
/// <param name="allfriends">list of friends</param>
/// <returns>task to be awaitable method</returns>
public void AddFacebookFriends(List<Friend> allfriends)
{
this.allfriends = allfriends;
//create client connexion
GraphRequest request = GraphRequest.NewMyFriendsRequest(AccessToken.CurrentAccessToken, this);
Bundle parameters = new Bundle();
parameters.PutString("fields","id,name");
request.Parameters = parameters;
request.ExecuteAsync();
}
public async void OnCompleted(JSONArray p0, GraphResponse p1)
{
Console.WriteLine(p1.ToString());
//get result from facebook
dynamic friends = JsonConverter.GenericJsonConverter(p0);
List<FbUser> fbUsers = new List<FbUser>();
//push all facebook friend in a list
foreach (var friend in friends["data"].Children())
{
FbUser fbUser = new FbUser();
fbUser.Id = friend["id"].ToString().Replace("\"", "");
fbUser.Name = friend["name"].ToString().Replace("\"", "");
fbUsers.Add(fbUser);
}
if (allfriends.Count > 0)
{
//get all friends object matching the facebook ids
var list = from first in allfriends
join second in fbUsers
on first.User.Facebook_Id
equals second.Id
select first;
//get the friendID of the current user
var myFriendId = allfriends.FirstOrDefault(f => f.User.Id == User.Instance.Id).Id;
List<UserFriends> userFriends = new List<UserFriends>();
foreach (var item in list)
{
//Link the current user with this friend
UserFriends userFriend = new UserFriends();
userFriend.FriendID = item.Id;
userFriend.UserID = User.Instance.Id;
userFriends.Add(userFriend);
//Link this friend to the current user
userFriend = new UserFriends();
userFriend.FriendID = myFriendId;
userFriend.UserID = item.User.Id;
userFriends.Add(userFriend);
}
var playingfriends = await ManageFriends.CreateFriend(userFriends);
Friends = Friend.Instance.CreateListFriend(playingfriends);
await ManageFriends.CreateFriend(userFriends);
}
}
}
如果有人已經實現了這個王的東西在xamarin Android和有一個代碼示例,這將是非常有益的。
謝謝。
感謝您的回答,我之前沒有找到此信息。我將在明天測試 – OrcusZ
CompletedHandlers在請求對象中不可用 – OrcusZ
感謝您的解釋,我按照您的步驟(重新實現)了界面,現在所有工作都正常。 – OrcusZ