Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

c# - Register Background Task in Silverlight 8.1 app

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

run faster

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...