這是在做一個聊天軟件的時候遇到的問題:與多個人聊天需要彈出多個窗口,但是與同一個聊天的窗口只能是一個
我將聊天窗口的Name定義成ChatWindow,另外在打開聊天窗口時,根據聊天對象設置聊天窗口的Title
另外定義了一個 i 這個i我覺得很好用
View Code
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 }
