Metro Universal Apps and Background Tasks
How do I call code from my shared project to run in the background of a Windows Phone project?
To run the code in the background of WP, I need to extract it into a new project and call this new project from my WP project. But to run the code, I need:
- Extract the logic to a background project (but then I lose the benefit of a collaborative project)
- Duplicate code
Is there a way to make a Universal Windows (Phone) app that executes some business logic in the background, without duplicating code or losing the benefit of a collaborative project?
Edit: This is the code that gets called when I put it in a separate project. It doesn't get called when I put it in a WP project or shared project.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Windows.ApplicationModel.Background;
namespace WindowsPhoneProject
{
public sealed class BackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
taskInstance.GetDeferral().Complete();
}
}
}
source to share
This Quickstart looks at ways to create background tasks for Universal Apps: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977055.aspx
You should be able to create a class that derives from IBackgroundTask in your shared project. If you want to create a background job on the phone, you can use the compile time directive like this:
#if WINDOWS_PHONE_APP
// Code Here
#endIf
source to share