如何创建一个静态全局事件,以便一个表单可以监听另一个不是父表单的表单事件?

我正在使用 WinForms,我有 3 种形式。FormA、FormB 和 FormC。

FormA 创建 FormB,FormB 创建 FormC。

我需要 FormA 知道 FormC 何时关闭。FormB 没有参与。我还有一个每个表单都可以访问的全局静态类。

我已经查看了如何使用另一种形式正确地侦听表单事件在 C# 中将事件从一种形式传播到另一种形式,但它们仅适用于 FormB 侦听 FormC。

如何在全局静态类中创建一个事件,以便 FormA 可以监听 FormC?

谢谢


holdtom
浏览 106回答 3
3回答

青春有我

static避免类和全局引用几乎总是 100% 更好。最好传递一个Action<FormC>允许您通知FormC即将关闭的消息。尝试这个:FormApublic partial class FormA : Form{&nbsp; &nbsp; public FormA()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; }&nbsp; &nbsp; private void Button1_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var formB = new FormB(this.ClosingC);&nbsp; &nbsp; &nbsp; &nbsp; formB.Show();&nbsp; &nbsp; }&nbsp; &nbsp; private void ClosingC(FormC formC)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Closing C");&nbsp; &nbsp; }}FormBpublic partial class FormB : Form{&nbsp; &nbsp; public FormB()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; }&nbsp; &nbsp; private Action<FormC> _closingFormC = null;&nbsp; &nbsp; public FormB(Action<FormC> closingFormC)&nbsp; &nbsp; &nbsp; &nbsp; : this()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _closingFormC = closingFormC;&nbsp; &nbsp; }&nbsp; &nbsp; private void Button1_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var formC = new FormC(_closingFormC);&nbsp; &nbsp; &nbsp; &nbsp; formC.Show();&nbsp; &nbsp; }}FormCpublic partial class FormC : Form{&nbsp; &nbsp; public FormC()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; }&nbsp; &nbsp; private Action<FormC> _closingFormC = null;&nbsp; &nbsp; public FormC(Action<FormC> closingFormC)&nbsp; &nbsp; &nbsp; &nbsp; : this()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _closingFormC = closingFormC;&nbsp; &nbsp; }&nbsp; &nbsp; private void FormC_FormClosing(object sender, FormClosingEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _closingFormC?.Invoke(this);&nbsp; &nbsp; }}

汪汪一只猫

我会使用事件聚合。这是一个小的Install-Package Caliburn.Micro.EventAggregator曾经像public class MyForm : Form, IHandle<MyEvent>{&nbsp; &nbsp;public MyForm()&nbsp;&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; SomeStaticPlace.EventAggregator.Subscribe(this); //Can be replaced with DI&nbsp; &nbsp;}&nbsp; &nbsp;public void Handle(MyEvent message)&nbsp;&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; //Act on event&nbsp; &nbsp;}}并发布SomeStaticPlace.EventAggregator.Publish(new MyEvent(...));

烙印99

这是带有静态事件的全局静态类FormClosed:public static class GlobalStaticClass{&nbsp; &nbsp; public static event FormClosedEventHandler FormClosed;&nbsp; &nbsp; public static void OnFormClosed(object sender, FormClosedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; FormClosed?.Invoke(sender, e);&nbsp; &nbsp; }}这是FormC。引发FormClosed事件:public partial class FormC : Form{&nbsp; &nbsp; public FormC()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; this.FormClosed += (sender, e) =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GlobalStaticClass.OnFormClosed(sender, e);&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}这是FormA听这个事件的:public partial class FormA : Form{&nbsp; &nbsp; public FormA()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; GlobalStaticClass.FormClosed += (sender, e) =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if (sender is FormC)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show(((Form)sender).Name + " Closed, reason: " + e.CloseReason);&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP