Background Agents in Windows phone 8.1 (Silverlight )

余生颓废 提交于 2019-12-10 09:44:02

问题


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 ?


回答1:


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>


来源:https://stackoverflow.com/questions/26933884/background-agents-in-windows-phone-8-1-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!