2015-12-02 190 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