-3

我试图在 VB 中翻译这段代码,但我不知道该怎么做……谁能告诉我翻译如何?

谢谢

private void WatchForDrives()
{
  DeviceStatusMonitor monitor = new DeviceStatusMonitor(DeviceClass.FileSystem, false);
  monitor.StartStatusMonitoring();
  monitor.DeviceNotification += delegate(object sender, DeviceNotificationArgs e)
    { 
    string message = string.Format("Disk '{0}' has been {1}.", e.DeviceName,       e.DeviceAttached ? "inserted" : "removed");
    MessageBox.Show(message, "Disk Status");
    };
 }
4

2 回答 2

1

http://converter.telerik.com/

Private Sub WatchForDrives()
    Dim monitor As New DeviceStatusMonitor(DeviceClass.FileSystem, False)
    monitor.StartStatusMonitoring()
    monitor.DeviceNotification += Function(sender As Object, e As DeviceNotificationArgs) Do
        Dim message As String = String.Format("Disk '{0}' has been {1}.", e.DeviceName, If(e.DeviceAttached, "inserted", "removed"))
        MessageBox.Show(message, "Disk Status")
    End Function
End Sub
于 2012-08-02T16:07:05.467 回答
1

只有 VB 10 支持多行 lambda,因此您必须创建一个单独的方法来处理事件。无论编译器如何,这都应该有效:

Private Sub WatchForDrives()
    Dim monitor As New DeviceStatusMonitor(DeviceClass.FileSystem, False)
    monitor.StartStatusMonitoring()
    AddHandler monitor.DeviceNotification, AddressOf MonitorDeviceNotified
End Sub

Private Sub MonitorDeviceNotified(ByVal sender As Object, ByVal e As DeviceNotificationArgs)
    Dim message As String = String.Format("Disk '{0}' has been {1}.", e.DeviceName, If(e.DeviceAttached, "inserted", "removed"))
    MessageBox.Show(message)
End Sub
于 2012-08-02T16:27:42.237 回答