Background Agents in Windows phone 8.1 (Silverligh

2019-05-16 09:15发布

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. : enter image description here

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 ?

1条回答
乱世女痞
2楼-- · 2019-05-16 09:53

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>
查看更多
登录 后发表回答