Ejemplo n.º 1
0
        private void StartTalk(object sender, MouseButtonEventArgs e)
        {
            var item       = (ListBoxItem)sender;
            var talkToUser = (TalkUser)item.Tag;

            if (!talkToUser.IsOnline)
            {
                MessageBox.Show(talkToUser.UserName + "未登录,不能进行聊天");
                return;
            }

            TalkWindow tw = IsTalkWindowOpen(talkToUser.UserName);

            if (tw == null)
            {
                //打开聊天窗口
                tw = new TalkWindow(_userName, talkToUser.UserName);
                //弹出显示在父窗口中间
                tw.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                //tw.Owner = this;
                tw.Name = talkToUser.UserName + "_TalkWindow";
                //弹出弹出后必须先关闭弹出才能操作父窗口
                tw.Show();
            }
            else
            {
                tw.Activate();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 判断聊天窗口是否打开,如果打开则返回窗口对象,否则返回null
        /// </summary>
        /// <param name="talkToUserName"></param>
        private TalkWindow IsTalkWindowOpen(string talkToUserName)
        {
            TalkWindow openWindow = null;

            System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
            {
                foreach (Window win in Application.Current.Windows)
                {
                    if (win.Name == talkToUserName + "_TalkWindow")
                    {
                        openWindow = (TalkWindow)win;
                        break;
                    }
                }
            }));

            return(openWindow);
        }
Ejemplo n.º 3
0
        private void ReceiveData()
        {
            while (true)
            {
                try
                {
                    var getMsg    = _clientInstance.br.ReadString();
                    var unPackMsg = TcpHelper.UnPackCommond(getMsg);
                    var commond   = unPackMsg.Item1;
                    switch (commond)
                    {
                    case TcpHelper.TalkCommond.Logout:

                        //获取好友列表
                        var sendCommond = TcpHelper.PackCommmond(_userName, TcpHelper.TalkCommond.UpdateUserList);
                        _clientInstance.SendMessage(sendCommond);

                        break;

                    case TcpHelper.TalkCommond.UpdateUserList:
                        try
                        {
                            //获取用户列表
                            var userList = JsonHandler.UnJson <List <TalkUser> >(unPackMsg.Item2);

                            //线程中不能操作UI
                            System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
                            {
                                CleanUserList();
                                userList = userList.OrderByDescending(v => v.IsOnline).ToList();
                                foreach (var user in userList)
                                {
                                    AddUserToList(user);
                                }
                            }));
                        }catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                        }


                        break;

                    case TcpHelper.TalkCommond.Talk:
                        var talkMsgs         = unPackMsg.Item2.Split(',');
                        var talkFromUserName = talkMsgs[0];
                        var talkContent      = unPackMsg.Item2.Substring(talkMsgs[0].Length + 1, unPackMsg.Item2.Length - talkMsgs[0].Length - 1);

                        //将消息存入临时列表
                        StoreTalkMessage(talkFromUserName, talkContent);

                        //判断聊天窗口是否打开
                        TalkWindow tw = IsTalkWindowOpen(talkFromUserName);
                        if (tw == null)
                        {
                            //如果聊天窗口没有打开,则将显示消息条数提示
                            var messageCount = MessageList.memory[talkFromUserName].Count();
                            MessageList.SetMessageTipList(talkFromUserName, messageCount);
                            MessageList.resetMessageTip = true;
                        }

                        break;

                    default:
                        MessageBox.Show(getMsg);
                        break;
                    }
                }catch (Exception e)
                {
                    _clientInstance.CloseConnect();
                    return;
                }
            }
        }