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 中调用的事件处理程序,其中 someNewEvent 在类文件之外,实际上是在 form.cs 中:

// 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 回答
0

您需要在 HIDNewDeviceEventMonitor 类中执行以下操作:

1.)首先在类中定义一个公共事件,如下所示 -

public event EventHandler Inserted; 2.) 然后在您检测到事件更改的代码中触发此事件。像这样-

if(Inserted != null) Inserted(this,null);

if 条件检查事件是否由任何侦听器注册。以防万一,它被解雇了。希望这可以帮助。

于 2014-12-03T13:51:32.740 回答