这是在做一个聊天软件的时候遇到的问题:与多个人聊天需要弹出多个窗口,但是与同一个聊天的窗口只能是一个
我将聊天窗口的Name定义成ChatWindow,另外在打开聊天窗口时,根据聊天对象设置聊天窗口的Title
另外定义了一个 i 这个i我觉得很好用

1 /// <summary> 2 /// 双击人员名单列表项 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 void lstChatters_MouseDoubleClick(object sender, MouseButtonEventArgs e) 7 { 8 Person person = lstChatters.SelectedItem as Person; 9 string title = "Chat with " + person.Name; 10 int i = 0; 11 foreach (Window win in Application.Current.Windows)//获取当前应用程序实例化窗口 12 { 13 string type = win.GetType().ToString(); 14 if (type == "Hayu2012.ChatForm") 15 { 16 if (String.Compare(win.Name, "ChatWindow") == 0 && String.Compare(win.Title, title) == 0) 17 { 18 i++; 19 win.Activate(); 20 return; 21 } 22 } 23 } 24 if (i == 0) 25 { 26 ChatForm cf = new ChatForm(); 27 cf.CurrentPerson = currPerson; 28 cf.OtherPerson = lstChatters.SelectedItem as Person; 29 cf.Title = title; 30 cf.Show(); 31 return; 32 } 33 }