新建委托类:
public class DataChangeEventArgs : EventArgs
{
// 定义委托
public delegate void DataChangeHandler(object sender, DataChangeEventArgs args);
public string devA { get; set; }
public string devB { get; set; }
public DataChangeEventArgs(string s1, string s2)
{
devA = s1;
devB = s2;
}
}
主窗口中定义
public void DataChangeDev(object sender, DataChangeEventArgs args) {
try
{
// 更新窗体控件
int a, b;
a = Convert.ToInt32(args.devA);
b = Convert.ToInt32(args.devB);
}
catch {
}
}
private void DevClick(object sender, RoutedEventArgs e)
{
DevSet devset = new DevSet();
devset.DataChange += new DataChangeHandler(DataChangeDev);
devset.textBox_Adevnum.Text = "0";
devset.textBox_Bdevnum.Text = "0";
devset.Top = this.Height / 3;
devset.Left = this.Height / 2;
devset.ShowDialog();
}
子窗口中
public partial class DevSet : Window
{
public event DataChangeHandler DataChange;// 声明事件
// 调用事件函数
public void OnDataChange(object sender, DataChangeEventArgs args)
{
if(DataChange != null)
{
DataChange(this, args);
}
}
public DevSet()
{
InitializeComponent();
}
private void button_set_Click(object sender, RoutedEventArgs e)
{
try
{
// this.Owner.
// 触发事件, 传递自定义参数
OnDataChange(this, new DataChangeEventArgs(textBox_Adevnum.Text, textBox_Bdevnum.Text));
this.Close();
}
catch {
}
}
}