在 2 wpf windows 之间发送信息
Send information between 2 wpf windows
我们有 2 个 windows 打开,喜欢聊天
这是文本框和按钮的样子:
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" 发送文本框中插入的信息。并打印到另一个window。
我一直在寻找类似 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();
}
}
}
聊天窗口
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)
{
}
}
}
首先,您应该研究与 XAML 的绑定,例如 here。这样,在您的 C# 代码中,您无需关心使用的 UI 控件——如果您不喜欢某些内容或想要改进,您可以轻松地更改 XAML 中的那些控件你的 window.
你只需要把你的MainWindow 和ChatWindow 想成对象。您可以通过多种方式完成这项工作。最简单的方法之一是在您的聊天 window 中创建一个事件,您的主要 window 在创建聊天 window 时订阅该事件。每当用户输入他的消息时,聊天 window 引发事件并通过事件中的参数传递文本,主要 window 捕获然后可以调用方法(或设置 属性) 在所有聊天 windows 中它都在跟踪,以便消息传递到所有聊天 windows。
一个简单的例子(自由打字,未经测试):
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;}
}
我设置了一些使用事件的示例代码:
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);
}
}
我会这样做:
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();
我们有 2 个 windows 打开,喜欢聊天
这是文本框和按钮的样子:
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" 发送文本框中插入的信息。并打印到另一个window。
我一直在寻找类似 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();
}
}
}
聊天窗口
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)
{
}
}
}
首先,您应该研究与 XAML 的绑定,例如 here。这样,在您的 C# 代码中,您无需关心使用的 UI 控件——如果您不喜欢某些内容或想要改进,您可以轻松地更改 XAML 中的那些控件你的 window.
你只需要把你的MainWindow 和ChatWindow 想成对象。您可以通过多种方式完成这项工作。最简单的方法之一是在您的聊天 window 中创建一个事件,您的主要 window 在创建聊天 window 时订阅该事件。每当用户输入他的消息时,聊天 window 引发事件并通过事件中的参数传递文本,主要 window 捕获然后可以调用方法(或设置 属性) 在所有聊天 windows 中它都在跟踪,以便消息传递到所有聊天 windows。
一个简单的例子(自由打字,未经测试):
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;}
}
我设置了一些使用事件的示例代码:
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);
}
}
我会这样做:
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();