// 选择不同的联系人时,右侧页面的更新 private void ContactList_SelectionChanged(object sender, SelectionChangedEventArgs e) { int index = contactList.SelectedIndex; if (index == -1) { return; } AddressBook.Item itemToShow = viewModel.addressBook.items[index]; showStack.Children.Clear(); showStack.VerticalAlignment = VerticalAlignment.Center; showStack.HorizontalAlignment = HorizontalAlignment.Center; Type type = itemToShow.GetType(); foreach (PropertyInfo pi in type.GetProperties()) { string name = pi.Name; var value = pi.GetValue(itemToShow, null); var dockPanel = new DockPanel(); dockPanel.Children.Add(new Label() { Content = $"{name}: ", MinWidth = 100 }); dockPanel.Children.Add(new Label() { Content = value != null ? value.ToString() : "null", MinWidth = 100 }); showStack.Children.Add(dockPanel); } }
public void updateChatList(AddressBook.Item item) { var chatRoomViewModel = new ChatRoomViewModel(); chatRoomViewModel.addressInfo = item; chatRoomViewModel.id = item.UserName; chatRoomViewModels.Add(chatRoomViewModel); }
// 发送群聊消息 public static void SendGroupWord(string word, string destId) // destId 群Id { AddressBook.Item targetItem = null; foreach (var item in App.addressBook.items) { if (!item.isGroup) { continue; } if (item.UserName == destId) { targetItem = item; break; } } if (targetItem == null) { MessageBox.Show("您不属于当前群聊"); return; } // 因为群聊中可能存在陌生人,重新向服务器查询并发送 foreach (var item in targetItem.GroupUserName) { if (item == App.user.userName) { continue; } string recv = CSClient.getInstance().SendAMsg($"q{item}"); switch (recv) { case "error": MessageBox.Show("服务器查询错误"); continue; case "Please send the correct message.": MessageBox.Show("群聊中有未知用户"); continue; case "n": continue; default: break; } string groupId = destId; string singleUserId = item; byte[] data = AppProtocol.PackWord($"from:{App.user.userName}\n{word}", groupId, singleUserId); P2PSender.getInstance().SendData(data, recv, P2PListener.GENERALLISTENPORT); } }
// 好友添加,向服务器确认ID是否合法,合法后添加好友 private void AddBtn_Click(object sender, RoutedEventArgs e) { var btn = (Button)sender; string recv = CSClient.getInstance().SendAMsg($"q{(string)btn.Tag}"); string Alias = (string)btn.ToolTip; if (Alias == null || Alias.Length == 0) { Alias = (string)btn.Tag; } switch (recv) { case "error": MessageBox.Show("添加失败"); break; case "Please send the correct message.": MessageBox.Show("不存在此用户"); break; default: foreach (var item in viewModel.addressBook.items) { if (item.UserName == (string)btn.Tag) { MessageBox.Show("此用户已经是您的好友"); return; } } Model.AddressBook.Item newItem = new AddressBook.Item() { Alias = Alias, UserName = (string)btn.Tag, isOnline = (recv != "n"), IPAddress = recv != "n" ? recv : "" }; viewModel.addressBook.items.Add(newItem); MessageBox.Show("添加成功"); // 更新界面 contactList.ItemsSource = null; contactList.ItemsSource = viewModel.addressBook.items; break; } }