I'm building an IoT app that needs to detect when a removable device was inserted and removed. I'm getting the following error when trying to create a DeviceWatcher object.
The DeviceWatcher.CreateWatcher()
method is of type DeviceWatcher
. Why would I be getting this error?
I'm not sure what the problem is and I don't know how to solve this. Can anyone give insight here?
DeviceWatcher watcher = DeviceInformation.CreateWatcher(DeviceClass.PortableStorageDevice);
Error:
Cannot implicitly convert type 'Windows.Devices.Enumeration.DeviceWatcher' to 'NamespaceName.DeviceWatcher'
Application Type:
Windows 10 Background Application. Anniversary Edition.
Manifest Capability:
Removable Storage
Try implicit variable declaration:
FURTHER EXPLANATION
By implicit declaration you are actually letting the compiler to do its magic (an intelligent guess) and derive the type of the variable, being declared based on the data type returned on the right. Basically your mistake is the assumption that the
watcher
variable should be of typeDeviceWatcher
but it isn't. At least not the one you've typed. Ther is a probably a conflict within yourusing
statements, and theDeviceWatcher
defaults to something different thanWindows.Devices.Enumeration.DeviceWatcher
which is the correct type returned by theDeviceInformation.CreateWatcher()