我正在構建一個物聯網應用程序,需要檢測何時插入和移除可移動設備。嘗試創建DeviceWatcher對象時出現以下錯誤。不能隱式轉換類型Windows.Devices.Enumeration.DeviceWatcher到DeviceWatcher
DeviceWatcher.CreateWatcher()
方法的類型爲DeviceWatcher
。爲什麼我會得到這個錯誤?
我不確定是什麼問題,我不知道如何解決這個問題。任何人都可以在此提供見解
DeviceWatcher watcher = DeviceInformation.CreateWatcher(DeviceClass.PortableStorageDevice);
錯誤:
無法隱式轉換類型 'Windows.Devices.Enumeration.DeviceWatcher' 到 'NamespaceName.DeviceWatcher'
應用類型:
的Windows 10背景應用。週年紀念版。
艙單能力:
可移動存儲
這解決了這個問題,但爲什麼我需要隱式聲明呢? – Jnr
@Jnr,因爲您的代碼中有兩個名爲'DeviceWatcher'的類,名爲'NamespaceName.DeviceWatcher',另一個在框架中的'Windows.Devices.Enumeration.DeviceWatcher'中。當你輸入'DeviceWatcher'時,它使用了你的代碼中的一個,但是這個函數從框架中重新使用它。使用'var'可以自動從框架中選擇一個。 –