DeviceWatcher Updated 和 Removed 事件从不触发
DeviceWatcher Updated and Removed events never fire
我正在尝试使用 DeviceWatcher 来侦听 USB 设备。如果在我的应用程序启动时设备已插入,则 Added 事件会很好地触发并且我可以连接。但是,如果我在我的应用程序启动后插入设备,或者如果我拔下设备,则不会触发其他事件。我已将回调添加到已添加、已删除和已更新,这就是文档所说的需要回调才能正常工作的内容。我错过了什么?
private void Watcher_DeviceAdded(DeviceWatcher sender, DeviceInformation deviceInfo) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
_resultCollection.Add(deviceInfo);
RaiseDeviceChanged(sender, deviceInfo.Id);
}
}
private void Watcher_DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
// Find the corresponding updated DeviceInformation in the collection and pass the update object
// to the Update method of the existing DeviceInformation. This automatically updates the object
// for us.
foreach (var deviceInfoDisp in _resultCollection) {
if (deviceInfoDisp.Id == deviceInfoUpdate.Id) {
deviceInfoDisp.Update(deviceInfoUpdate);
RaiseDeviceChanged(sender, deviceInfoUpdate.Id);
break;
}
}
}
}
private void Watcher_DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
// Find the corresponding DeviceInformation in the collection and remove it
foreach (var deviceInfoDisp in _resultCollection) {
if (deviceInfoDisp.Id == deviceInfoUpdate.Id) {
_resultCollection.Remove(deviceInfoDisp);
break;
}
}
RaiseDeviceChanged(sender, deviceInfoUpdate.Id);
}
}
我意识到我将错误的 VID 和 PID 传递给了观察者。我正在使用 Windows.Devices.SerialCommunication.SerialDevice
class 获取设备选择器,我将错误的 ID 传递给了静态方法。
我正在尝试使用 DeviceWatcher 来侦听 USB 设备。如果在我的应用程序启动时设备已插入,则 Added 事件会很好地触发并且我可以连接。但是,如果我在我的应用程序启动后插入设备,或者如果我拔下设备,则不会触发其他事件。我已将回调添加到已添加、已删除和已更新,这就是文档所说的需要回调才能正常工作的内容。我错过了什么?
private void Watcher_DeviceAdded(DeviceWatcher sender, DeviceInformation deviceInfo) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
_resultCollection.Add(deviceInfo);
RaiseDeviceChanged(sender, deviceInfo.Id);
}
}
private void Watcher_DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
// Find the corresponding updated DeviceInformation in the collection and pass the update object
// to the Update method of the existing DeviceInformation. This automatically updates the object
// for us.
foreach (var deviceInfoDisp in _resultCollection) {
if (deviceInfoDisp.Id == deviceInfoUpdate.Id) {
deviceInfoDisp.Update(deviceInfoUpdate);
RaiseDeviceChanged(sender, deviceInfoUpdate.Id);
break;
}
}
}
}
private void Watcher_DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
// Watcher may have stopped while we were waiting for our chance to run.
if (IsWatcherStarted(sender)) {
// Find the corresponding DeviceInformation in the collection and remove it
foreach (var deviceInfoDisp in _resultCollection) {
if (deviceInfoDisp.Id == deviceInfoUpdate.Id) {
_resultCollection.Remove(deviceInfoDisp);
break;
}
}
RaiseDeviceChanged(sender, deviceInfoUpdate.Id);
}
}
我意识到我将错误的 VID 和 PID 传递给了观察者。我正在使用 Windows.Devices.SerialCommunication.SerialDevice
class 获取设备选择器,我将错误的 ID 传递给了静态方法。