在两个WPF窗口之间发送事件 [英] Sending event between 2 WPF windows
本文介绍了在两个WPF窗口之间发送事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望配置窗口发送和事件到主窗口。
任何想法我如何实现?
这是我如何创建和打开配置窗口:
private void MenuItem_Click(object sender,RoutedEventArgs e)
{
ConfigPage Confwin = new ConfigPage();
Confwin.Owner = this;
Confwin.Show();
}
解决方案
您可以定义任何事件
主窗口
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事件
MessageBox.Show(Handle);
}
}
子窗口 p>
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)
{
//调用事件
this.OnMyEvent();
}
}
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.
Main 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");
}
}
Child window
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();
}
}
这篇关于在两个WPF窗口之间发送事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文