我实现了一种方法来获取已安装应用程序的所有朋友。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