1

I want to know how can I declarate in the EntryPoint of a Declaration in the manifest file the location of the code to execute as a background task.

I have my code set in a folder called "Models" inside the project, But I don't know how to reffer to this code. Here you have a picture of it:

enter image description here

Just in case, here is my code inside the cs:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.UI.Popups;

namespace Universal_in_C.Models
{
    public sealed class ExampleBackgroundTask : IBackgroundTask
    {
        public async Task ExampleMethodAsync()
        {
            Debug.WriteLine("Done Exe");
            var dialog = new MessageDialog("TESTING, TESTING LIKE THERE IS NO TOMORROW.");
            await dialog.ShowAsync();
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
            await ExampleMethodAsync();
            _deferral.Complete();
        }
    }
}

And how I call it(I know that I need to change the entry here too);

private void button6_Click(object sender, RoutedEventArgs e)
{
    var taskRegistered = false;
    var exampleTaskName = "BackgroundTask";

    foreach (var task in BackgroundTaskRegistration.AllTasks)
    {
        if (task.Value.Name == exampleTaskName)
        {
            taskRegistered = true;
            break;
        }
    }

    if (taskRegistered)
    {
        Debug.WriteLine("Already Exist");
    }
    else
    {
        var builder = new BackgroundTaskBuilder();
        Debug.WriteLine("Started to Exist");
        builder.Name = exampleTaskName;
        builder.TaskEntryPoint = "exampleTaskName";
        builder.SetTrigger(new SystemTrigger(SystemTriggerType.InternetAvailable, true));
        builder.AddCondition(new SystemCondition(SystemConditionType.UserPresent));
        builder.Register();
    }


}

Thanks!

Onelio
  • 313
  • 1
  • 4
  • 16

1 Answers1

3

From what I understand, you are having problems declaring the background task on the manifest file. Based on this, my answer.

  1. You need to create a new project that will contain your background tasks. This is necessary in order to correctly debug them later on.
  2. Reference the project on your application.
  3. Create the task by implementing the IBackgroundTask interface.
  4. Register it via code.
  5. Register it on the manifest.

At step number 5, the entry point is always the {project namespace}.{background task name}, so for example if you created a project names BackgroundTasks with a task ExampleTask, the entry point will be: BackgroundTasks.ExampleTask

I'll leave for future references a link to the guidelines and tutorial on how to implement them.

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30