C# 在静态类中引发事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/881034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:12:48  来源:igfitidea点击:

raising events in a static class

c#eventsclassstatic

提问by

I have a windows form with a data grid bound to a datatable. On a button click, the data table is passed to a static class.

我有一个带有绑定到数据表的数据网格的 Windows 窗体。单击按钮时,数据表将传递给静态类。

private void btnSave_ItemClick(object sender, EventArgs e)
{
   MyStaticClass.SaveData(DataTable dt);
}


internal static class MyStaticClass
{
    internal static void SaveData(DataTable dt)
    {
       foreach(DataRow dr in dt.rows)
       {
           // do something    
       }
    }
}

I need to pass back status information from the SaveData method back to my form so that I can inform the user how record is being processed.

我需要将状态信息从 SaveData 方法传回我的表单,以便我可以通知用户如何处理记录。

say for example - I want to send back a message every 100 records - "processing record #...." so that it is displayed on the form.

举个例子——我想每 100 条记录发回一条消息——“处理记录 #....”,以便它显示在表单上。

is it possible to raise an event from a static class?

是否可以从静态类引发事件?

回答by Hemanshu Bhojak

Yes, you can raise an event from your static class. Define the event as static.

是的,您可以从静态类中引发事件。将事件定义为静态。

public static event EventHandler MyEvent;

回答by Fredrik M?rk

Yes it is possible and works just like events in non-static classes do (except that you, of course, need to declare the event with as being static).

是的,它是可能的,并且就像非静态类中的事件一样工作(当然,除了您需要将事件声明为 is static)。

Note though that through this design, you can (at least in theory) have several forms calling SaveDatasimultanteously, so one instance of the method is raising events targeted for Form A, while another instance will be raising events targeted for Form B. Since the event is static, all events will be caught by both forms, so you may want to include information in event that the form can use to determine whether a particular invocation of the event is of interest or not.

请注意,通过这种设计,您可以(至少在理论上)SaveData同时调用多个表单,因此该方法的一个实例正在引发针对 Form A 的事件,而另一个实例将引发针对 Form B 的事件。由于事件是静态的,所有事件都将被两种表单捕获,因此您可能希望在事件中包含信息,表单可使用这些信息来确定是否对特定的事件调用感兴趣。

You could, for instance, put together a custom EventArgs class and pass the DataTablein that, so that the listening code can see if it is the DataTable from that form. If it is, the UI will be updated, if it's not the call can simply be ignored.

例如,您可以将一个自定义 EventArgs 类放在一起并传入该类DataTable,以便侦听代码可以查看它是否是该表单中的 DataTable。如果是,UI 将被更新,如果不是,调用可以简单地被忽略。

回答by Dirty

Please someone correct me if I'm wrong! I think, by convention the delegate that handles the event should be declared with two arguments:

如果我错了,请有人纠正我!我认为,按照惯例,处理事件的委托应该用两个参数声明:

public delegate void SomethingHappenedEventHandler(object sender, EventArgs args); 

Normally the sender object is "this", but in a static class, there is no such thing as "this" :-( I have therefore created an empty dummy sender-object to send off with the event, to keep it compatible with all the other EventHandlers.

通常发件人对象是“this”,但在静态类中,没有“this”这样的东西:-(因此我创建了一个空的虚拟发件人对象来发送事件,以保持它与所有其他事件处理程序。

//Events:
    public delegate void SomethingHappenedEventHandler(object sender, EventArgs args); //1. Define a delegate
    public static event SomethingHappenedEventHandler SomethingHappened;                  //2. Define an event based on that delegate
    private static void OnSomethingHappened()
    {
        Object sender = new Object();
        SomethingHappened?.Invoke(sender, EventArgs.Empty);                            //3. raise the event

But I am just a beginner. So, if this is utter nonsense, please let me know why.

但我只是一个初学者。所以,如果这完全是胡说八道,请告诉我为什么。