Register a background task in Silverlight 8.1

I am working on an application that uses BLE to communicate with an element, and I need to get a background notification from it. I am aware of the existence of a GattCharacteristicNotificationTrigger , but I cannot find a way to register a background job in Silverlight 8.1.

What advice?

+6
source share
1 answer

Registering BackgroundTask is pretty well explained here on MSDN .

Here is a simple example given in TimeTrigger that shows Toast steps (applicable to both RunTime and Silverlight applications):

  1. 1. BackgroungTask should be a component of Windows Runtime (regardless of whether your application is Runtime or Silverlight). To add a new one, right-click on your solution in the Solution Explorer window in VS, select
Add , then New project and select the Windows Runtime component.

winRTcomponent

  1. 2. Add the link to your main project.

addreference

  1. 3. Indicate
Declarations in the Package.appxmanifest file - you need to add the Backgorund task, mark the timer and specify the entry point for the task. The entry point will be Namespace.yourTaskClass (which implements IBackgroundTask ) - an added component of Windows Runtime.

declaration

  1. 4. What might your BackgroundTask look like? - let's say we want to send a toast from it (of course, it can be a lot of 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)); } } } 

  1. 5. Finally, let
will register our BackgroundTask in the 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 run. As you can see, the task should start after 15 minutes (this time may vary depending on how the OS assigns the task at certain time intervals, so it works between 15-30 minutes). But how to debug a task faster?

There is an easy way - go to the "Debugging" toolbar and you will see the drop-down events of the life cycle, select your task from it, and it will open (sometimes open / close) to update it.

run faster

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

+22
source

Source: https://habr.com/ru/post/970413/


All Articles