在 2 个 WPF 窗口之间发送事件 [英] Sending event between 2 WPF windows
本文介绍了在 2 个 WPF 窗口之间发送事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个 WPF 窗口.一个是主窗口,另一个是一些配置窗口.我希望配置窗口将事件发送到主窗口.知道我是如何完成它的吗?
I have two WPF windows. One is the main window and the other is some configuration window. I would like the configuration window to send and event to the main window. Any idea how I an accomplish it?
这是我创建和打开配置窗口的方式:
This is how I create and open the configuration window :
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
ConfigPage Confwin= new ConfigPage();
Confwin.Owner = this;
Confwin.Show();
}
推荐答案
您可以在子窗口类中定义任何事件并在显示窗口之前订阅它.
You can define any event in your child window class and subscribe to it before showing the window.
主窗口
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Child childWindow = new Child();
childWindow.MyEvent += new EventHandler(childWindow_MyEvent);
childWindow.ShowDialog();
}
void childWindow_MyEvent(object sender, EventArgs e)
{
// handle event
MessageBox.Show("Handle");
}
}
子窗口
public partial class Child : Window
{
// define event
public event EventHandler MyEvent;
protected void OnMyEvent()
{
if (this.MyEvent != null)
this.MyEvent(this, EventArgs.Empty);
}
public Child()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Child_Loaded);
}
void Child_Loaded(object sender, RoutedEventArgs e)
{
// call event
this.OnMyEvent();
}
}
这篇关于在 2 个 WPF 窗口之间发送事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文