UWP SerialDevice.FromIdAsync抛出“找不到元素". (Windows 10的HRESULT例外:0x80070490) [英] UWP SerialDevice.FromIdAsync throws "Element not found" (Exception from HRESULT: 0x80070490) on Windows 10

查看:508
本文介绍了UWP SerialDevice.FromIdAsync抛出“找不到元素". (Windows 10的HRESULT例外:0x80070490)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Xamarin Forms应用程序中打开连接的蓝牙设备上的串行端口.

I want to open serial port on an attached Bluetooth device in a Xamarin Forms application.

下面是代码(为了说明问题,我对其进行了简化):

Here is the code (I simplified it in order to illustrate the problem):

  string l_gdsSelector = SerialDevice.GetDeviceSelector();
  var l_ardiDevices = await DeviceInformation.FindAllAsync(l_gdsSelector);

  foreach(DeviceInformation l_diCurrent in l_ardiDevices)
  {
    if(l_diCurrent.Name.StartsWith("PX05"))
    {
      m_sdDevice = await SerialDevice.FromIdAsync(l_diCurrent.Id);

      break;
    }
  }

此代码抛出找不到元素"(HRESULT的异常:0x80070490)await SerialDevice.FromIdAsync

This code throws "Element not found" (Exception from HRESULT: 0x80070490) Exception at await SerialDevice.FromIdAsync

我不敢相信:找不到元素",而DeviceInformation.FindAllAsync只是将其作为现有设备返回了!

I cannot believe it : "Element not found" whereas DeviceInformation.FindAllAsync juste returned it as an existing device !

有人可以向我解释这种奇怪的行为吗?以及主要如何解决?

Can someone explain me this strange behavior? and predominatingly how to solve it?

提前谢谢

推荐答案

对DeviceInformation.FindAllAsync函数的首次调用必须在UI线程中进行.由于此代码是DLL的一部分,因此我决定始终在UI线程中调用它.

The first call to DeviceInformation.FindAllAsync function must be made in UI thread. as this code is a part of a DLL, I decided to always call it in the UI thread.

这是我修改的代码:

  TaskCompletionSource<bool> l_tcsResult = new TaskCompletionSource<bool>();

  await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
    Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
      try
      {
        m_sdDevice = await SerialDevice.FromIdAsync(p_strDeviceID);

        l_tcsResult.SetResult(true);
      }
      catch (Exception l_exError)
      {
        l_tcsResult.SetException(l_exError);

        System.Diagnostics.Debug.WriteLine(l_exError);
      }
    });

  await l_tcsResult.Task;

要允许应用程序与串行设备通信,请编辑软件包清单并在以下部分中添加以下条目:

To allow application to communicate with serial device, please edit package manifest and add following entry in section:

<DeviceCapability Name="serialcommunication">
  <Device Id="any">
    <Function Type="name:serialPort" />
  </Device>
</DeviceCapability>

这篇关于UWP SerialDevice.FromIdAsync抛出“找不到元素". (Windows 10的HRESULT例外:0x80070490)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆