Beispiel #1
0
        private async void Btnadd_Click(object sender, RoutedEventArgs e)
        {
            if (IsSuccess)
            {
                try
                {
                    AVUser friend = await AVUser.Query.WhereEqualTo("username", friendname.Text).FirstAsync();

                    //先检查对方是否已经为好友
                    if (Conversation.MemberIds.Contains(friend.ObjectId))
                    {
                        MessageBox.Show("你们已经是好友啦");
                    }

                    else
                    {
                        //不是好友再检测对方是否在线
                        List <string> friends = new List <string>
                        {
                            friend.ObjectId
                        };
                        if ((await User.PingAsync(targetClientIds: friends)).First().Item2)
                        {
                            //在线
                            await User.CreateConversationAsync(friend.ObjectId, isUnique : true);

                            MessageBox.Show("添加成功,等待对方确认");
                            Close();
                        }
                        else
                        {
                            //不在线
                            MessageBox.Show("对方不在线,请稍后重试");
                        }
                    }
                }
                catch (AVException error)
                {
                    MessageBox.Show(error.Message);
                }
            }
            else
            {
                Friendname_LostFocus(new object(), new RoutedEventArgs());
            }
        }
Beispiel #2
0
        //绑定好友信息
        private async void BindingFriendListSrouce(string newfriend = null)
        {
            //完全重新加载list
            if (newfriend == null)
            {
                //如果有好友的话
                if (FriendsConversation.MemberIds != null)
                {
                    //TODO:优化成add
                    MyFriendsList.Clear();
                    //显示自己的好友
                    foreach (var online in await User.PingAsync(targetClientIds: FriendsConversation.MemberIds))
                    {
                        if (online.Item1 != AVUser.CurrentUser.ObjectId)
                        {
                            var user = await AVUser.Query.GetAsync(online.Item1);

                            //将获取到的好友信息保存到列表中,根据client判断用户是否在线
                            MyFriendsList.Add(new UserList(user, online.Item2));
                        }
                    }
                    //如果有离线信息的话,将离线的消息保存==》TODO:离线消息顺序有变化,待解决
                    if (OfflineMessageList != null)
                    {
                        App.Current.Dispatcher.Invoke((Action)(() =>
                        {
                            OfflineMessageList.ForEach((messgae) =>
                            {
                                MyFriendsList.ForEach((Friend) =>
                                {
                                    if (Friend.Friend.ObjectId == messgae.Message.FromClientId)
                                    {
                                        //将消息加入对应好友的消息列表
                                        Friend.MessageList.Add(new Message(messgae.Message));
                                        //在主页面对应用户上做提示
                                        Friend.Visibility = Visibility.Visible;
                                    }
                                });
                            });
                            //最后清空离线消息
                            OfflineMessageList = null;
                        }));
                    }
                }
            }
            //仅add
            else
            {
                List <string> friends = new List <string>
                {
                    newfriend
                };
                foreach (var online in await User.PingAsync(targetClientIds: friends))
                {
                    if (online.Item1 != AVUser.CurrentUser.ObjectId)
                    {
                        var user = await AVUser.Query.GetAsync(online.Item1);

                        //将获取到的好友信息保存到列表中,根据client判断用户是否在线
                        MyFriendsList.Add(new UserList(user, online.Item2));
                    }
                }
            }
            //将列表中的好友信息绑定到listbox里
            friendlist.ItemsSource = null;
            friendlist.ItemsSource = MyFriendsList;
        }