简体   繁体   English

如何在 MAUI 中启动 DeviceWatcher?

[英]How to start DeviceWatcher in MAUI?

So I've managed to create a platform-specific service which task is to communicate with an USB device via HID.所以我设法创建了一个特定于平台的服务,其任务是通过 HID 与 USB 设备进行通信。 As the platform-specific code doesn't work as described in MAUI docs, I've done it by DI in app builder: .AddSingleton<IHidCommunicationService, HidCommunicationService>();由于特定于平台的代码无法按照 MAUI 文档中的描述工作,因此我在应用程序构建器中通过 DI 完成了它: .AddSingleton<IHidCommunicationService, HidCommunicationService>(); . . Thanks to this mate:) 感谢这位朋友:)

The communication works fine but now I want to refresh devices list at runtime and to do so I wish to use DeviceWatcher in my HidCommunicationService .通信工作正常,但现在我想在运行时刷新设备列表,为此我希望在我的HidCommunicationService中使用DeviceWatcher And here's the issue: when I try to fire the device watcher by using Start() method I get this error: A method was called at an unexpected time .这就是问题所在:当我尝试使用Start()方法触发设备观察程序时,出现此错误:在意外时间调用了方法

I've tried the same code in WPF app and it works fine:我在 WPF 应用程序中尝试了相同的代码,它工作正常:

string aqsFilter = HidDevice.GetDeviceSelector(usagePage, usageId, vendorId, productId);
DeviceWatcher deviceWatcher = DeviceInformation.CreateWatcher(aqsFilter);
deviceWatcher.Start();

The deviceWatcher.Start() has to be fired after adding at least one event handler to deviceWatcher 's events: Added , Removed or Updated .在向deviceWatcher的事件添加至少一个事件处理程序后,必须触发deviceWatcher.Start()AddedRemovedUpdated

WinRT information: At least an Added, Updated or Removed event handler must be registered before a watcher can be started. WinRT 信息:至少必须注册一个已添加、已更新或已删除的事件处理程序,然后才能启动观察程序。 A method was called at an unexpected time.在意外的时间调用了方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM