C# 接口事件的实际使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1745990/
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-06 20:25:48  来源:igfitidea点击:

Practical use of interface events

c#eventsinterfacedelegates

提问by user160677

What is a good example of the power of interface events (declaring events inside interface)?

什么是接口事件(在接口内声明事件)的力量的好例子?

Most of the times I have seen only public abstract methods inside interface.

大多数时候我只看到接口中的公共抽象方法。

采纳答案by Daniel A. White

I used events to signal when a serial port received data.

当串行端口接收到数据时,我使用事件来发出信号。

Here is my interface.

这是我的界面。

public interface ISerialPortWatcher
{
    event EventHandler<ReceivedDataEventArgs> ReceivedData;
    event EventHandler StartedListening;
    event EventHandler StoppedListening;

    SerialPortSettings PortOptions { set; }

    bool Listening { get; set; }
    void Stop();
    void Start();
}

public class ReceivedDataEventArgs : EventArgs
{
    public ReceivedDataEventArgs(string data)
    {
        Data = data;
    }
    public string Data { get; private set; }
}

回答by Bob

Events in interfaces work pretty much just like methods. You can use them just how you would use any interface.

接口中的事件与方法非常相似。您可以像使用任何界面一样使用它们。

public interface IInterface {
    event EventHandler QuestionAsked;
}

public class Class : IInterface {
    event EventHandler QuestionAsked;

    //As with typical events you might want an protected OnQuestionAsked
}

回答by Andrew Keith

here is one example

这是一个例子

public interface IMainAppWindow
{
   event EventHandler Closed;
}

// version 1 main window
public MainForm : Form , IMainAppWindow
{

}

// version 2 main window
public MainWindow : Window , IMainAppWindow
{
  event EventHandler Closed;

  public void OnClosed(object sender,RoutedEventArgs e)
  {
    if(Closed != null)
    {
      Closed(this,e);
    }
  }
}

I have some code like this in 1 of my applications. The app was written in winforms, then upgraded to WPF.

我的应用程序 1 中有一些这样的代码。该应用程序是用 winforms 编写的,然后升级到 WPF。

回答by Andrew Shepherd

An excellent example within the .NET framework is the INotifyPropertyChangedinterface. This interface consists of only one member: the PropertyChangedevent.

.NET 框架中的一个很好的例子是INotifyPropertyChanged接口。此接口仅包含一个成员:PropertyChanged事件。

In WPF, you can state that a control will display a specific property of an object instance. But how will this control update if the underlying property changes?

在 WPF 中,您可以声明控件将显示对象实例的特定属性。但是,如果基础属性发生变化,此控件将如何更新?

If the bound object implements the INotifyPropertyChangedinterface, the WPF framework can just listen to PropertyChangedand update appropriately.

如果绑定对象实现了INotifyPropertyChanged接口,则 WPF 框架可以只侦听PropertyChanged并进行适当的更新。

回答by Rohan West

INotifyPropertyChangedis used through out the framework.

INotifyPropertyChanged用于整个框架。

Just look at the INotifyPropertyChanged.PropertyChangedEvent

看看INotifyPropertyChanged.PropertyChanged事件

回答by softveda

A classic scenario is MVP pattern with passive view. The form implememts an view inteface that has a NameChanged event. The presenter that creates/uses the view subscribes to this event. When the name text in textbox is changed view fires this event. The presenter is then notified. Since the presenter only knows about event from view interface you can provide a mock view for testing. The view is completely decoupled from presenter.

一个经典的场景是具有被动视图的 MVP 模式。该表单实现了一个具有 NameChanged 事件的视图接口。创建/使用视图的演示者订阅此事件。当文本框中的名称文本更改时,视图会触发此事件。然后通知演示者。由于演示者仅从视图界面了解事件,因此您可以提供模拟视图进行测试。视图与演示者完全分离。