0

知識が浅いので許してください!

HIDNewDeviceEventMonitor.cs に次のクラスがあります。

public class HIDNewDeviceEventMonitor : IDisposable
{
    // used for monitoring plugging and unplugging of USB devices.
    private ManagementEventWatcher watcherAttach;

    public HIDNewDeviceEventMonitor()
    {
        // Catch USB HID plugged instance event watching
        watcherAttach = new ManagementEventWatcher();
        watcherAttach.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
        watcherAttach.Query = new WqlEventQuery(@"SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_PNPEntity' AND TargetInstance.DeviceID LIKE 'HID\\VID_04D8%'");
        watcherAttach.Start();
    }

    void watcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        Debug.WriteLine("my device is inserted..");
    }

    public void Dispose()
    {
        watcherAttach.Stop();
        watcherAttach.Dispose();
    }

    ~HIDNewDeviceEventMonitor()
    {
        this.Dispose();
    }
}

さて、このクラスを変更して、クラスがwatcher_EventArrived内から呼び出すことができるイベントハンドラーを追加できるようにするにはどうすればよいですか

// code in the form
HIDNewDeviceEventMonitor ok = new HIDNewDeviceEventMonitor();
ok.Inserted += someNewEvent;  // <-- my problem, I don't know how to add an event to the class this way

private void someNewEvent()
{
    //Enumerate and add to listbox1
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    ok.Dispose();
}

私は他のクラスでこのことを見てきました。どうすれば私のクラスをそのようにすることができますか?

4

3 に答える 3

2

イベントInsertedは次のようになります。

public event EventHandler Inserted;

次のように呼び出します。

private void OnInserted()
{
    if (this.Inserted != null)
    {
        this.Inserted(this, EventArgs.Empty);
    }
}

イベント ハンドラーのシグネチャは次のとおりです。

void someNewEvent(object sender, EventArgs e)
{
    //
}

次に、そのコードをクラスのコンストラクターでラップする必要があります。

HIDNewDeviceEventMonitor ok;

public ClassName()
{
   ok = new HIDNewDeviceEventMonitor();
   ok.Inserted += someNewEvent;  // <-- my problem
}

okコンストラクタの外側で変数を宣言し、内側でインスタンス化します。次に、イベント ハンドラーを追加します。

プロのヒント:EventHandler<T>のカスタム実装を提供する必要がある場合は、ジェネリックを使用できますe

于 2014-12-03T13:30:44.293 に答える
1

簡単に言えば、HIDNewDeviceMonitorクラスにイベントを追加しようとしています。

これを行うには、まずデリゲートを定義する必要があります。

public delegate void InsertedHandler;

次に、HIDNewDeviceMonitorクラスでイベントを定義する必要があります。

// Notice how the event uses the delegate that's been defined
//               v       
public event InsertedHandler Inserted;

ここで、イベントを「起動」するものが必要になります。これは、メソッドに簡単に入れることができますwatcher_EventArrived

void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
    Debug.WriteLine("my device is inserted..");

    // Notice how we check the event handler for null.
    // If you don't, it could throw a NullReferenceException.
    // Irritating, but easy to debug.. Usually..
    if (Inserted != null)
        Inserted(); // Or whatever parameters you need.
}

私たちは皆、HIDNewDeviceMonitor授業を終えました。

これで、 を使用するどのクラスでも、HIDNewDeviceMonitor提供した EventHandler コードを使用できます。

ただし、同じデリゲートである必要があります。

public class MyClass
{
  HIDNewDeviceMonitor monitor;
  public MyClass()
  {
    monitor = new HIDNewDeviceMonitor();
    monitor.Inserted += DeviceInserted;
  } 

  private void DeviceInserted() 
  { 
   // Execute code here
  }
}
于 2014-12-03T13:42:39.377 に答える