手动发射在C#中的事件 [英] manually firing the event in c#

查看:118
本文介绍了手动发射在C#中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想手动使用C#触发事件。例如,说,如果我想从表B.火A型的Form_closing事件怎么办呢?

i want to fire an event manually using c#. For instance, say if i want to fire a Form_closing event of Form A from Form B. How to do it ??

得到一些意见之后。我想我需要更多地解释这一点。

After getting some comments. I think i need to explain more on this.

由于我的A型是参考它创建桌面自定义任务栏上一个.dll,
有我的情况,从表b
I关闭该自定义任务栏已经从表B.试图FormA.Close()当我这样做,将.dll是从应用程序域卸载,由于这种占用的空间通过自定义任务栏被阻止。

Since my Form A is reference to a .dll which creates a custom taskbar in the desktop, There is a situation for me to close the that custom taskbar from Form B. i already tried FormA.Close() from Form B. when i do this, the .dll is unloaded from the app domain and due to this the space occupied by the custom task bar is blocked.

但是,这并不是当我单击自定义任务栏中的关闭按钮的情况。当我做到这一点的空间被释放。

But that is not the case when i click the close button in the custom task bar. when i do this the space is freed up.

这是我想从表格B,这将解决我的问题,手动触发型A的关闭事件的原因。

This is the reason i want to fire the close event of Form A manually from Form B which will solve my issue.

感谢

推荐答案

我们做了以下一个项目:

We did the following in one project:

有一个GlobalNotifier类,它定义的事件,我们希望在应用程序的不同模块来使用,这样

There was a GlobalNotifier class, which defined events we wanted to use in different modules of the application, like this

public static class GlobalNotifier
{

    public static event VoidEventHandler EnvrionmentChanged;

    public static void OnEnvrionmentChanged()
    {
        if (EnvrionmentChanged != null)
        {
            EnvrionmentChanged();
        }
    }
 }



然后,你可以提出这个事件的任何地方,当你需要让应用程序的其余部分知道,环境变了,像这样

Then, you could raise this event anywhere when you needed to let the rest of the application know that the environment has changed, like this

    GlobalNotifier.OnEnvrionmentChanged();



,你也可以订阅该事件无论你想被通知有关的环境有事实。更改

And also you could subscribe to this event wherever you wanted to be notified about the fact that the environment has changed.

    public ReportingService()
    {
        GlobalNotifier.EnvrionmentChanged += new VoidEventHandler(GlobalNotifier_EnvrionmentChanged);
    }

    void GlobalNotifier_EnvrionmentChanged()
    {
        //reset settings
        _reportSettings = null;
    }



所以,当你改变了的环境中,你提到的事件,大家谁了解这一点,执行一些操作需要,被通报。
可能是相似的,你需要达到什么

So, whenever you changed the environment, you raised the event, and everyone who needed to know about that and perform some actions, was notified. Might be similar to what you need to achieve.

另外,如果你需要传递参数,你可以定义事件中,你喜欢的任何方式,基本上是 -

Also, if you need to pass parameters, you can define the event any way you like, basically -

    public static event VoidEventHandler<SomeObject, List<OtherObject>> SomethingUpdated;

    public static void OnSomethingUpdated(SomeObject sender, List<OtherObject> associations)
    {
        if (SomethingUpdated != null)
        {
            SomethingUpdated(sender, associations);
        }
    }

    // ...

    MyClass.SomethingUpdated+= new VoidEventHandler<SomeObject, List<OtherObject>>(MyClass_SomethingUpdated);

    // ...

    void MyClass_SomethingUpdated(SomeObject param1, List<OtherObject> param2)
    {
      //do something
    }

这篇关于手动发射在C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆