Benjamin Diele
Benjamin Diele

Reputation: 1187

Universal Metro apps and background tasks

How do I call code from my Shared Project to run in the background of the Windows Phone project?

To run code in the background on WP, I need to extract that to a new project and call that new project from my WP project. But to run the code I'd need to either:

Is there a way to make a Universal Windows (Phone) App that executes some business logic in the background, without duplicating the code or losing the advantage of a Shared Project?

Edit: This is the code that is called when I put it in a seperate project. It is not called when I put it in either the WP project, or the 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();
        }
    }
}

Upvotes: 3

Views: 885

Answers (1)

Bret Bentzinger
Bret Bentzinger

Reputation: 376

This Quickstart walks through how 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 the class that derives from IBackgroundTask in your shared project. If you only want to create the background task on the phone, then you can use a compile time directive like so:

#if WINDOWS_PHONE_APP
//  Code Here
#endIf

Upvotes: 1

Related Questions