BackgroundTaskBuilder.Register 抛出异常 [英] BackgroundTaskBuilder.Register throws exception

查看:23
本文介绍了BackgroundTaskBuilder.Register 抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 GattCharacteristicNotifictionTrigger 注册一个后台任务以从 BLE 设备接收数据,但无论我做什么,应用程序在到达 时总是抛出异常.Register() 行.我参考了 MSDN 在其他地方,我相信我正在做所有需要的事情.下面是一个核对清单,突出显示了所需的内容.就后台任务而言,我什么也没做:

I'm trying to register a background task with the GattCharacteristicNotifictionTrigger to receive data from a BLE device, but no matter what I do, the app ALWAYS throws an exception when it reached the .Register() line. I've referred to numerous guides on MSDN and on other places and I believe I'm doing everything that's needed. Below is a checklist highlighting what was needed. I've done nothing more and nothing less as far as the background task goes:

1) 在 Package.appxmanifest 中设置后台任务的声明,如下所示:

1) Set the declaration for the background task in Package.appxmanifest like so:

      <Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="AgHost.BackgroundTask">
      <BackgroundTasks>
      <Task Type="systemEvent" />
      </BackgroundTasks>
    </Extension>
    <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundBLEService.MyBLEService">
      <BackgroundTasks>
        <m3:Task Type="gattCharacteristicNotification" />
      </BackgroundTasks>
    </Extension>
  </Extensions>

2) 添加对 WinRT 项目的引用,该项目在我的主应用程序项目(顺便说一句是 Silverlight 8.1 项目)中具有后台任务

2) Add a reference to the WinRT project which has the background task in my main app project (which is a Silverlight 8.1 project btw)

3) 在后台服务类(它是一个密封的公共类)中创建一个 Run() 函数,如下所示:

3) Create a Run() function within the background service class (which is a sealed, public class) like so:

public sealed class MyBLEService: IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        try
        {
            GattCharacteristicNotificationTriggerDetails details = (GattCharacteristicNotificationTriggerDetails)taskInstance.TriggerDetails;
            byte[] ReceivedData = new byte[details.Value.Length];
            DataReader.FromBuffer(details.Value).ReadBytes(ReceivedData);

            foreach(byte b in ReceivedData)
            {
                Debug.WriteLine(b.ToString());
                XmlDocument xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
                xml.SelectSingleNode("/toast/visual/binding/text").InnerText = string.Format("Value Received: " + b.ToString());
                ToastNotification toast = new ToastNotification(xml);
                ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier();
                notifier.Show(toast);
            }
        }
        finally
        {
            Debug.WriteLine("Background service exception");
        }
    }
}

4) 从我的主应用程序中注册事件,如下所示:

4) Register the event from within my main app like so:

            GattCharacteristicNotificationTrigger trigger = new GattCharacteristicNotificationTrigger(thermometerCharacteristic); //thermometerCharacteristic is defined in another block of code
            await BackgroundExecutionManager.RequestAccessAsync();
            BackgroundTaskBuilder BLETaskBuilder = new BackgroundTaskBuilder();
            BLETaskBuilder.Name = "DataReceiveNotifier";
            BLETaskBuilder.TaskEntryPoint = "BackgroundBLEService.MyBLEService";
            BLETaskBuilder.SetTrigger(trigger);
            App.MyBackgroundTask = BLETaskBuilder.Register();

5) 确保后台任务入口点的字符串序列完全匹配后台项目的 namespace.classname 拼写.我已经检查并仔细检查了 Package.appxmanifest 和我试图注册任务的主项目中的拼写.

5) Make sure the string sequence for the background task entry point exactly matches the namespace.classname spelling of the background project. I've checked and double checked the spellings both in the Package.appxmanifest and in my main project where I'm attempting to register the task.

但是当代码遇到 .Register() 行时,我仍然遇到第一次机会异常.有关异常的更多详细信息:

But I'm still getting a first chance exception when the code hits the .Register() line. More details about the exception:

  • HResult:-2147221164
  • 消息:类未注册(来自 HRESULT 的异常:0x80040154 (REGDB_E_CLASSNOTREG))

知道我做错了什么吗?我在这里找到了一个类似的问题,但没有明确回答.

Any idea what I'm doing wrong? I found a similar question here, but it's not definitely answered.

推荐答案

以防万一...

当您使用 builder.SetTrigger(new TimeTrigger(15, false)) 时,您需要检查 Timer 以及 System Event在您的 Package.appxmanifest 文件中.它对我有用.

When you use builder.SetTrigger(new TimeTrigger(15, false)) you will need to check Timer as well as System Event in your Package.appxmanifest file. It was working for me.

这篇关于BackgroundTaskBuilder.Register 抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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