简体   繁体   中英

Background Agents in Windows phone 8.1 (Silverlight )

I am following this link for implementing the ScheduledAgent in WP 8.1 Silverlight.

Steps :-

Edited WMAppManifest.xaml :

<Tasks>
  <DefaultTask Name="_default" NavigationPage="/View/StartPage.xaml" />
  <ExtendedTask Name="BackgroundTask">
    <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="ScheduledTaskAgent2" Source="ScheduledTaskAgent2" Type="ScheduledTaskAgent2.ScheduledAgent" />
  </ExtendedTask>
</Tasks>

Added new ScheduledAgent project with targeted version 8.1. : 在此输入图像描述

Now my ScheduledAgent Class

#define DEBUG_AGENT
using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;

namespace ScheduledTaskAgent2
{
    public class ScheduledAgent : ScheduledTaskAgent
    {

         protected override void OnInvoke(ScheduledTask task)
         { 

#if DEBUG_AGENT
            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(60));
#endif
            NotifyComplete();         

          }
    }
}

My code to start the agent

public const string PeriodicTaskName = "ScheduledTaskAgent2";
private PeriodicTask _periodicTask;

    private void StartPeriodicAgent()
    {
        _isPeriodicTaskStarted = true;

        _periodicTask = ScheduledActionService.Find(PeriodicTaskName) as PeriodicTask;

        if (_periodicTask != null)
        {
            RemoveAgent(PeriodicTaskName);
        }

        _periodicTask = new PeriodicTask(PeriodicTaskName) {Description = "periodic task."};

        try
        {
            ScheduledActionService.Add(_periodicTask);

#if(DEBUG_AGENT)
            ScheduledActionService.LaunchForTest(PeriodicTaskName, TimeSpan.FromSeconds(60));
#endif
         }
         catch (Exception exception){ }
    }         

    private static void RemoveAgent(string name)
    {
       try
       {
           ScheduledActionService.Remove(name);
       }
       catch (Exception){}
    }

Now this all that I have tried for the background agent. This is not invoking the OnInvoke() Method (at least in debug mode)

Note : I have added the Reference to ScheduledTaskAgent2 project also.

Has anybody implemented the ScheduleAgent in WP 8.1 (Silverlight)

Is it supported at all ?

I got the solution This is the fully working solution just copy paste it. Can't get it from documentation directly though. Just Add this Extention in your Package.appxmanifest File. you can open it by right click => viewcode option.

 <Extension Category="windows.backgroundTasks" EntryPoint="AgHost.BackgroundTask">
      <BackgroundTasks>
        <Task Type="systemEvent"  />
        <Task Type="timer"/>
      </BackgroundTasks>
    </Extension>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM