我设法枚举我的USB设备,如下所示。如何将enumerateDevices添加到DeviceWatcher中? 我参考了以下示例,但仍未取得任何进展。 观察者在启动期间确实触发了一次,随后我的usb设备中的任何更改都没有显示任何内容。
[如果添加,删除或更改设备(XAML)如何获取通知] [1] [设备枚举样本] [2]知道如何持续监控USB端口上的更改,以提示是否有任何USB设备被移除或连接?
private void StartWatcher()
{
usbStorageWatcher = DeviceInformation.CreateWatcher(DeviceClass.PortableStorageDevice);
audioCaptureWatcher = DeviceInformation.CreateWatcher(DeviceClass.AudioCapture);
audioRenderWatcher = DeviceInformation.CreateWatcher(DeviceClass.AudioRender);
usbStorageWatcherAdded = new TypedEventHandler<DeviceWatcher, DeviceInformation>(async (watcher, deviceInfo) =>
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, async () =>
{
var usbDevices = await DeviceInformation.FindAllAsync(DeviceClass.PortableStorageDevice);
if (usbDevices.Count > 0)
{
for (var i = 0; i < usbDevices.Count; i++)
{
usbDeviceList.Add(usbDevices[i]);
}
usbList.SelectedItem = usbDevices[0];
usbDeviceCount.Text = usbDevices.Count.ToString();
GeneralMessage.Text = String.Format("{0} USB Storage found.", usbDevices.Count);
}
});
});
usbStorageWatcher.Added += usbStorageWatcherAdded;
usbStorageWatcherRemoved = new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>(async (watcher, deviceInfoUpdate) =>
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, async () =>
{
var usbDevices = await DeviceInformation.FindAllAsync(DeviceClass.PortableStorageDevice);
if (usbDevices.Count > 0)
{
for (var i = 0; i < usbDevices.Count; i++)
{
usbDeviceList.Add(usbDevices[i]);
}
usbList.SelectedItem = usbDevices[0];
usbDeviceCount.Text = usbDevices.Count.ToString();
GeneralMessage.Text = String.Format("{0} USB Storage found.", usbDevices.Count);
}
});
});
usbStorageWatcher.Removed += usbStorageWatcherRemoved;