2015-12-02 116 views
0

我們有2把窗戶打開,就像一個聊天發送信息WPF窗口

enter image description here

這是文本框什麼,該按鈕看起來像:

private void textBox_chat_TextChanged(object sender, TextChangedEventArgs e) 
{ 
} 

private void button_enviar_Click(object sender, RoutedEventArgs e) 
{ 
    string chatMessage = textBox_chat.Text; 
} 

我想知道如何通過按下「button_enviar」按鈕來發送文本框中的信息。並打印到其他窗口。 我一直在尋找像Application.Current.Windows ...但仍然沒有找到辦法。

我的代碼實際上是這樣的:

主窗口

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Lógica de interacción para MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
     { 
      public MainWindow() 
      { 
       InitializeComponent(); 
      } 

      // automatic code generated by the button 
      private void button_entrar_Click(object sender, RoutedEventArgs e) 
      { 
       // we catch the taxt input in the texBox 
       string userLoginName = textBox_pantalla_inicial.Text; 

       // We call the chat window 
       Window window1 = new Window1(); 
       // we put the user name as the title of the chat window 
       window1.Title = userLoginName; 
       // show the chat window 
       window1.Show();    
      }  
     } 
    } 

ChatWindow

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Lógica de interacción para Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      // inicialize chatWindow 
      InitializeComponent();    
     } 

     private void textBox_chat_TextChanged(object sender, TextChangedEventArgs e) 
     { 

     } 

     private void button_enviar_Click(object sender, RoutedEventArgs e) 
     { 
      string chatMessage = textBox_chat.Text; 

     }  

     private void button_erase_Click(object sender, RoutedEventArgs e) 
     { 

     } 
    } 
} 
+0

你可以看看你的應用程序中託管一個WCF服務並調用服務來顯示消息。 –

+0

WCF - Windows Communication Foundation https://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx在.NET中查找聊天教程 – Paparazzi

+0

感謝您的反饋,但需要解決方案完全在本地工作... – Qu4k3

回答

1

首先,你應該看看與XAML結合,如here。這種方式在你的C#代碼中,你不需要關心使用的UI控件 - 如果你不喜歡某些東西或者想改善你的窗口,你可以很容易地更改XAML中的這些控件。

您只需要將您的MainWindow和ChatWindow視爲對象。有很多方法可以使這項工作。最簡單的方法之一是在您的主窗口創建聊天窗口時訂閱的聊天窗口中有一個事件。無論何時用戶輸入他的消息,聊天窗口都會引發事件並通過事件中的參數傳遞文本,主窗口捕獲該參數,然後可以在所有正在跟蹤的聊天窗口中調用方法(或設置屬性),以便該消息將傳遞給所有聊天窗口。

一個簡單的例子(無類型的,未測試):

public class MainWindow : Window 
{ 
    List<ChatWindow> chatWindows = new List<ChatWindow>(); 
    public void AddChatWindow() 
    { 
     ChatWindow win = new ChatWindow(); 
     win.NewMessage += MessageReceived; 
     win.Show(); 
     chatWindows.Add(win); 
    } 
    void MessageReceived(object sender, MessageEventArgs e) 
    { 
     ChatWindow me = sender as ChatWindow; 
     if (me != null) 
     { 
      foreach (ChatWindow win in chatWindows) 
      { 
       if (win != me) 
       { 
        win.Add(e.Message); 
       } 
      } 
     } 
    } 
} 

public class ChatWindow : Window 
{ 
    public event EventHandler<MessageEventArgs> NewMessage; 

    public void Add(string message) 
    { 
     Messsage += message; 
    } 
    public void UpdateText(string text) 
    { 
     if (NewMessage != null) 
     { 
      NewMessage(this, new MessageEventArgs(Message = text)); 
     } 
    } 
    public string Message {get;set;} 
} 
public class MessageEventArgs : EventArgs 
{ 
    public string Message{get;set;} 
} 
+0

我只是用我的代碼來實現我的帖子,你的帖子真的很接近我,如果你能看一下我的代碼,我會加以讚賞。 – Qu4k3

+0

非常感謝您的幫助,它幫助我澄清了我的疑惑! – Qu4k3

1

我設置使用的事件一個小例子代碼:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    ChatMsgDispacher _chatMsgDispacher = new ChatMsgDispacher(); 
    public ChatChild GetNewChat() 
    { 
     var child = new ChatChild(); //or where you create the child 
     child.SetMsgDispacher(_chatMsgDispacher); 
     return child; 
    } 
} 

public class ChatMsgDispacher 
{ 
    public delegate void ChatMsgDelegate(string msg); 
    public event ChatMsgDelegate MessageUpdate; 

    public void Update(string msg) 
    { 
     if (MessageUpdate != null) 
     { 
      MessageUpdate(msg); 
     } 
    } 
} 


public class ChatChild 
{ 
    private ChatMsgDispacher _msgDispacher; 

    public void SetMsgDispacher(ChatMsgDispacher msgDispacher) 
    { 
     _msgDispacher = msgDispacher; 
     _msgDispacher.MessageUpdate += MsgDispacher_MessageUpdate; 
    } 

    void MsgDispacher_MessageUpdate(string msg) 
    { 
     //add the msg in the child view 
    } 

    private void button_enviar_Click(object sender, RoutedEventArgs e) 
    { 
     string chatMessage = textBox_chat.Text; 
     _msgDispacher.Update(chatMessage); 
    } 
} 
+0

感謝您的反饋!我會看看你的代碼 – Qu4k3

2

這是我會怎麼做:

public partial class ChatWindow : Window 
{ 

    private Client client; 
    public ChatWindow(Client _client) 
    { 
     InitializeComponent(); 
     client = _client; 
     this.Title = client.Name + " chat"; 
     client.MessageReceived += OnMessageReceived; 


     this.Loaded += OnLoaded; 
    } 

    public void OnMessageReceived(object sender, MessageReceivedArgs e) 
    { 
     chatControl.Text += e.Sender.Name+": "+ e.Message; 
    } 

    private void OnLoaded(object sender, EventArgs e) 
    { 
     client.Send("client " + client.Name + " is loaded!"); 
    } 


} 


public class Client{ 

    public string Name { get; set; } 
    public Chat chat{get;set;} 



    public Client(string name) 
    { 
     Name = name; 
    } 

    public delegate void MessageReceivedEventHandler(object sender, MessageReceivedArgs e); 

    public event MessageReceivedEventHandler MessageReceived; 

    private void RaiseMessageReceivedEvent(Client sender, string message) 
    { 
     MessageReceivedArgs e = new MessageReceivedArgs(sender,message); 
     if (MessageReceived != null) 
      MessageReceived(this, e); 
    } 

    public void MessageReceivedFromChat(Client sender,string message) 
    { 
     RaiseMessageReceivedEvent(sender,message); 
    } 

    public void Send(string message) 
    { 
     chat.SendMessage(this, message); 
    } 




} 

public class MessageReceivedArgs : EventArgs 
{ 
    public string Message { get; set; } 
    public Client Sender { get; set; } 
    public MessageReceivedArgs(Client sender,string message) 
    { 
     Message = message; 
     Sender = sender; 
    } 
} 


public class Chat 
{ 
    private List<Client> clients; 

    public Chat() 
    { 
     clients = new List<Client>(); 
    } 

    public void AddClient(Client client) 
    { 
     client.chat = this; 
     clients.Add(client); 
    } 

    public void RemoveClient(Client client) 
    { 
     client.chat = null; 
     clients.Remove(client); 
    } 

    public void SendMessage(Client sender, string message) 
    { 
     foreach(Client client in clients){ 
      if (client != sender) 
      { 
       client.MessageReceivedFromChat(sender, message); 
      } 

     } 
    } 

} 

創建對象:

 Chat chat = new Chat(); 
     Client jacques = new Client("jacques"); 
     Client Pierre = new Client("Pierre"); 
     chat.AddClient(jacques); 
     chat.AddClient(Pierre); 

     ChatWindow cw = new ChatWindow(jacques); 
     cw.Show(); 
     ChatWindow cw1 = new ChatWindow(Pierre); 
     cw1.Show(); 
+0

感謝您的反饋!我會看看你的代碼 – Qu4k3