UWP中无法使用Added和EnumerationCompleted事件的问题咨询
Hey there! Let's dig into why those Added and EnumerationCompleted events aren't firing for your IoT project. I've run into similar snags working with Microsoft's IoT SDKs, so here are the most common culprits and actionable fixes to try:
Common Issues & Fixes
1. You're registering events after starting the enumeration
This is the #1 mistake I see. If you start the device watcher/enumerator before hooking up your event handlers, you'll miss the initial batch of Added events and the EnumerationCompleted trigger entirely. Always register events first, then start the process.
Example of the correct order:
// First, create the watcher var deviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.IoTControlDevice); // Then hook up your events deviceWatcher.Added += DeviceWatcher_Added; deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted; // Finally, start the enumeration deviceWatcher.Start();
2. Your device selector is incorrect
If your selector doesn't match any IoT devices on your system, the Added event will never fire, and EnumerationCompleted will trigger immediately with no devices to report.
- Verify your selector by running a synchronous query first to check for devices:
var devices = await DeviceInformation.FindAllAsync(yourDeviceSelector); Debug.WriteLine($"Found {devices.Count} matching devices"); - Use specific selectors like
DeviceInformation.GetDeviceSelectorFromGuid(yourDeviceInterfaceGuid)instead of generic ones if you're targeting a specific hardware type.
3. The watcher object is being garbage-collected prematurely
If you create the device watcher as a local variable inside a method, it might get cleaned up by the GC before events can fire. Store it as a class-level member to keep it alive:
// Class-level variable to preserve the watcher instance private DeviceInformationWatcher _deviceWatcher; private void InitializeDeviceEnumeration() { _deviceWatcher = DeviceInformation.CreateWatcher(yourDeviceSelector); _deviceWatcher.Added += OnDeviceAdded; _deviceWatcher.EnumerationCompleted += OnEnumerationDone; _deviceWatcher.Start(); }
4. Missing permissions or manifest declarations (for UWP/desktop apps)
- For UWP projects: Ensure you've added the required device capabilities in your
Package.appxmanifest. For IoT devices, this often includes<DeviceCapability Name="lowLevel" />or specific hardware capabilities matching your device. - For desktop apps: Try running your application as an administrator—some IoT device enumeration requires elevated privileges.
5. Event handler signature doesn't match the delegate
Double-check that your event methods have the correct parameter types. For example, the Added event uses a TypedEventHandler<DeviceInformationWatcher, DeviceInformation> delegate, so your handler should look like:
private void OnDeviceAdded(DeviceInformationWatcher sender, DeviceInformation deviceInfo) { Debug.WriteLine($"New device found: {deviceInfo.Name}"); }
A mismatched signature (even a subtle one like wrong parameter names) can prevent the event from firing silently.
Full Working Example
Here's a complete, robust implementation you can adapt for your project:
using Windows.Devices.Enumeration; using System.Diagnostics; public class IoTDeviceScanner { private DeviceInformationWatcher _deviceWatcher; public void StartScanning() { // Replace with your target device selector string deviceSelector = DeviceInformation.GetDeviceSelectorFromDeviceClass(DeviceClass.IoTControlDevice); _deviceWatcher = DeviceInformation.CreateWatcher(deviceSelector); // Register all relevant events _deviceWatcher.Added += OnDeviceAdded; _deviceWatcher.EnumerationCompleted += OnScanCompleted; _deviceWatcher.Removed += OnDeviceRemoved; _deviceWatcher.Stopped += OnWatcherStopped; _deviceWatcher.Start(); Debug.WriteLine("Device scan started..."); } private void OnDeviceAdded(DeviceInformationWatcher sender, DeviceInformation deviceInfo) { Debug.WriteLine($"Added: {deviceInfo.Name} | ID: {deviceInfo.Id}"); // Add your device initialization logic here } private void OnScanCompleted(DeviceInformationWatcher sender, object args) { Debug.WriteLine("Initial device enumeration completed!"); // Run post-scan tasks here (e.g., update UI, connect to devices) } private void OnDeviceRemoved(DeviceInformationWatcher sender, DeviceInformationUpdate update) { Debug.WriteLine($"Removed device ID: {update.Id}"); } private void OnWatcherStopped(DeviceInformationWatcher sender, object args) { Debug.WriteLine("Device watcher stopped."); } public void StopScanning() { if (_deviceWatcher != null && _deviceWatcher.Status == DeviceWatcherStatus.Started) { _deviceWatcher.Stop(); // Clean up event handlers to avoid memory leaks _deviceWatcher.Added -= OnDeviceAdded; _deviceWatcher.EnumerationCompleted -= OnScanCompleted; _deviceWatcher.Removed -= OnDeviceRemoved; _deviceWatcher.Stopped -= OnWatcherStopped; _deviceWatcher = null; } } }
Quick Debugging Tips
- Check the
_deviceWatcher.Statusproperty in debug mode to confirm it's in theStartedstate. - Monitor the Output window for debug logs—this will help you confirm if events are firing or if the watcher is stopping unexpectedly.
- Test with a known working IoT device to rule out hardware-specific issues.
内容的提问来源于stack exchange,提问作者Sunday

