在2个WPF窗口之间发送信息 [英] Send information between 2 wpf windows

查看:185
本文介绍了在2个WPF窗口之间发送信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有2个打开的窗口,就像聊天室

We have 2 windows open, like a chat

这是文本框和按钮的外观:

This is what the textBox and the button looks like:

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 ...之类的东西,但仍然找不到实现它的方法。

I would like to know how can I send information insered in a textbox by pressing the button "button_enviar". And to be printed to the other window. I have been looking things like Application.Current.Windows... but still don't found the way to make it.

我的代码实际上是这样的:

My code looks actually like this:

MainWindow

MainWindow

   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

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

        }
    }
}


推荐答案

首先,您应该考虑使用XAML进行绑定,例如此处 。这样,在您的C#代码中,您无需关心所使用的UI控件,并且如果您不喜欢某些东西或想要改善窗口,则可以轻松地在XAML中更改这些控件。

First, you should look into binding with XAML, such as here. That way in your C# code, you won't need to care about the UI controls used--and you can easily change those controls in the XAML if you don't like something or want to improve your window.

您只需要将MainWindow和ChatWindow视为对象。有很多方法可以使这项工作。最简单的方法之一是在聊天窗口中有一个事件,您的主窗口在创建聊天窗口时会订阅该事件。每当用户输入消息时,聊天窗口都会引发事件并通过事件中的参数传递文本,主窗口会捕获该事件,然后可以在其跟踪的所有聊天窗口中调用方法(或设置属性),以便

You only need to think of your MainWindow and ChatWindow as objects. There are many ways you can make this work. One of the simplest ways is to have an event in your chat window that your main window subscribes to when it creates the chat window. Whenever a user enters his message, the chat window raises the event and passes the text through arguments in the event, which the main window catches and then can call a method (or set a property) in all the chat windows it is tracking so that the message gets passed to all chat windows.

一个简单的示例(自由类型,未经测试):

A simple example (free-typed, not tested):

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个WPF窗口之间发送信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆