订阅不相关的UserControl中的Form中定义的自定义事件

时间:2019-03-28 16:59:14

标签: c# winforms event-handling

我在Form中定义了一个自定义事件,我想在一个不相关(即不是该表单的子项)UserControl中进行订阅。 实际上,该事件从未在UserControl中处理 我想念什么?

EventTopicNames.cs

public class EventTopicNames
{
    public const string ApplicationClosing = "ApplicationClosing";
}

ShellApplication.cs

    internal class ShellApplication : SmartClientSplashApplication<Microsoft.Practices.CompositeUI.WorkItem, ShellForm, SplashScreen>
    {

    base.Shell.FormClosing += this.Shell_FormClosing

    private void Shell_FormClosing (object sender, System.Windows.Forms.FormClosingEventArgs formClosingEventArgs)
    {
        // notify all modules that the application is closing
        Shell.OnApplicationClosing (new System.EventArgs());

    }
}

ShellForm.cs

public partial class ShellForm : System.Windows.Forms.Form

        [Microsoft.Practices.CompositeUI.EventBroker.EventPublication (Interface.Constants.EventTopicNames.ApplicationClosing, Microsoft.Practices.CompositeUI.EventBroker.PublicationScope.Global)]
        public event System.EventHandler<System.EventArgs> ApplicationClosing;

        protected internal virtual void OnApplicationClosing (System.EventArgs eventArgs)
        {
            if (this.ApplicationClosing != null)
            {
                this.ApplicationClosing (this, eventArgs);
            }
        }

UnrelatedUserControl.cs

public partial class UnrelatedUserControl : System.Windows.Forms.UserControl
{
     [Microsoft.Practices.CompositeUI.Commands.CommandHandler(Infrastructure.Interface.Constants.EventTopicNames.ApplicationClosing)]
    protected internal virtual void OnApplicationClosing(object sender, Infrastructure.Interface.EventArgs<Infrastructure.Interface.Aml.BusinessEntities.Project> eventArgs)
    {
        // do stuff but never called
    }
}

0 个答案:

没有答案