在2个wpf窗口之间发送信息

时间:2015-12-02 15:53:08

标签: c# wpf chat active-window

我们打开了2个窗口,就像聊天一样

enter image description here

这就是textBox和按钮的样子:

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)
        {

        }
    }
}

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

首先,您应该研究与XAML的绑定,例如here。这样,在您的C#代码中,您不必关心所使用的UI控件 - 如果您不喜欢或想要改进窗口,您可以轻松更改XAML中的控件。 / p>

您只需要将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;}
}

答案 2 :(得分: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);
    }
}