在Silverlight 8.1应用程序中注册后台任务 [英] Register Background Task in Silverlight 8.1 app

查看:91
本文介绍了在Silverlight 8.1应用程序中注册后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用BLE与商品进行通信的应用,我需要从中接收背景通知。我知道 GattCharacteristicNotificationTrigger 的存在,但是找不到在Silverlight 8.1应用程序中注册后台任务的任何方法。



有什么建议吗?

解决方案

注册 :

 私有异步无效Button_Click(对象发送者,RoutedEventArgs e)
{
// Windows Phone应用必须调用它以使用触发器类型(请参见MSDN)
等待BackgroundExecutionManager.RequestAccessAsync();

BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder {名称=第一个任务,TaskEntryPoint = myTask.FirstTask};
taskBuilder.SetTrigger(new TimeTrigger(15,true));
BackgroundTaskRegistration myFirstTask = taskBuilder.Register();
}



编译,运行,它应该可以工作。如您所见,任务应在15分钟后开始(此时间可能会因操作系统在特定间隔内安排任务而有所不同,因此它将在15-30分钟之间触发)。但是如何更快地调试任务?



有一种简单的方法-转到调试位置工具栏,您将看到一个下拉菜单生命周期事件,选择从中执行您的任务,它将触发(有时会打开/关闭下拉菜单以刷新它)。





在这里您可以下载我的示例代码-WP8.1 Silverlight应用程序。


I'm working on an app that uses BLE to communicate with an item and I need to receive background notifications from it. I am aware of the existence of GattCharacteristicNotificationTrigger but I can't find any way to register a Background Task in a Silverlight 8.1 app.

Any tip?

解决方案

Registering a BackgroundTask is quite well explained here at MSDN.

Here is simple example fired upon TimeTrigger and showing a Toast, the steps are (applies to both - RunTime and Silverlight apps):

    1. BackgroungTask must be a Windows Runtime Componenet (no matter if your App is Runtime or Silverlight). To add a new one, right click on your Solution in Solution Explorer window in VS, select Add then New project and choose Windows Runtime Component.

    2. Add a reference in your main project.

    3. Specify Declarations in Package.appxmanifest file - you need to add a Backgorund Task, mark Timer and specify Entry Point for the Task. The Entry Point will be a Namespace.yourTaskClass (which implements IBackgroundTask) - the added Windows Runtime Component.

    4. How can your BackgroundTask look like? - let's say we want to send a Toast from it (of course it can be many other things):

    namespace myTask // the Namespace of my task 
    {
       public sealed class FirstTask : IBackgroundTask // sealed - important
       {
          public void Run(IBackgroundTaskInstance taskInstance)
          {
            // simple example with a Toast, to enable this go to manifest file
            // and mark App as TastCapable - it won't work without this
            // The Task will start but there will be no Toast.
            ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
            XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
            XmlNodeList textElements = toastXml.GetElementsByTagName("text");
            textElements[0].AppendChild(toastXml.CreateTextNode("My first Task"));
            textElements[1].AppendChild(toastXml.CreateTextNode("I'm message from your background task!"));
            ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
          }
       }
     }
    

    5. Finally, let's register our BackgroundTask in main project:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        // Windows Phone app must call this to use trigger types (see MSDN)
        await BackgroundExecutionManager.RequestAccessAsync();
    
        BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "First Task", TaskEntryPoint = "myTask.FirstTask" };
        taskBuilder.SetTrigger(new TimeTrigger(15, true));
        BackgroundTaskRegistration myFirstTask = taskBuilder.Register();
    }
    

Compile, run and it should work. As you can see the task should start after 15 minutes (this time can vary as OS schedules task in specific intervals, so it will fire between 15-30 minutes). But how to debug a task faster?

There is a simple way - go to Debug location toolbar and you will see a dropdown Lifecycle events, choose your task from it and it will fire (sometimes open/close dropdown to refresh it).

Here you can download my sample code - WP8.1 Silverlight App.

这篇关于在Silverlight 8.1应用程序中注册后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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