генерирование событий в статическом классе

Когда это происходит с нами на физических устройствах, мы обычно пытаемся это исправить, отключив его, включив «режим отладки Android» на устройстве и повторно подключившись.

7
задан Jon Skeet 19 May 2009 в 05:16
поделиться

2 ответа

Да, вы можете вызвать событие из своего статического класса. Определите событие как статическое.

public static event EventHandler MyEvent;
3
ответ дан 6 December 2019 в 19:41
поделиться

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).

Note though that through this design, you can (at least in theory) have several forms calling SaveData simultanteously, 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.

You could, for instance, put together a custom EventArgs class and pass the DataTable in 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.

10
ответ дан 6 December 2019 в 19:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: