我试图在Windows桌面WPF
C#应用程序中使用Windows 10 runtime Bluetooth Low-energy (BLE) APIs。代码主要基于Bluetooth Advertisement UWP sample,但我已经added the reference到相关的库。
但是,我遇到了BackgroundTaskRegistration
类的问题。
在我的RegisterBackgroundTask
方法中(与this基本相同),我尝试查看我的BLE扫描后台任务是否已存在:
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
if (cur.Value.Name == taskName)
{
// The task is already registered.
return (BackgroundTaskRegistration)(cur.Value);
}
}
但是在第一行,调用BackgroundTaskRegistration.AllTasks
会给出:
抛出异常:' System.Exception'在BLEProximity.exe中
消息:找不到元素。 (HRESULT的例外情况:0x80070490)
堆栈:在Windows.ApplicationModel.Background.BackgroundTaskRegistration.get_AllTasks() 在BLEProject.MainWindow.RegisterBackgroundTask(String taskEntryPoint,String taskName,IBackgroundTrigger trigger,IBackgroundCondition条件)在C:\ Users \ MyUserName \ Documents \ Visual Studio 2015 \ Projects \ BLEProject \ BLEProject \ MainWindow.xaml.cs:第421行
来源:Windows.ApplicationModel
如果我绕过上述检查后台任务的存在,当我调用Register()
的{{1}}方法时,我会收到同样的错误。
请注意,在BackgroundTaskBuilder
应用中使用此代码时,我发现无问题。有关更多背景信息,请参阅this related question。
我唯一的猜测是,这可能会以某种方式与应用程序清单相关,但我对Windows应用程序开发还是比较陌生的。