English 中文(简体)
Practical use of interface events [closed]
原标题:
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 8 years ago.

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.

最佳回答

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; }
}
问题回答

An excellent example within the .NET framework is the INotifyPropertyChanged interface. This interface consists of only one member: the PropertyChanged event.

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?

If the bound object implements the INotifyPropertyChanged interface, the WPF framework can just listen to PropertyChanged and update appropriately.

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.

INotifyPropertyChanged is used through out the framework.

Just look at the INotifyPropertyChanged.PropertyChanged Event

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
}

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签