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

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

问题描述

我正在开发一个使用 BLE 与项目通信的应用程序,我需要从它接收后台通知.我知道 GattCharacteristicNotificationTrigger 的存在,但我找不到在 Silverlight 8.1 应用程序中注册后台任务的任何方法.

有什么提示吗?

解决方案

注册 :

private async void Button_Click(object sender, RoutedEventArgs e){//Windows Phone 应用程序必须调用它才能使用触发器类型(参见 MSDN)等待 BackgroundExecutionManager.RequestAccessAsync();BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "First Task", 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天全站免登陆