简体   繁体   English

Windows Phone 8.1中的后台代理(Silverlight)

[英]Background Agents in Windows phone 8.1 (Silverlight )

I am following this link for implementing the ScheduledAgent in WP 8.1 Silverlight. 我正在关注此链接以在WP 8.1 Silverlight中实现ScheduledAgent。

Steps :- 脚步 :-

Edited WMAppManifest.xaml : 编辑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. 添加了具有目标版本8.1的新ScheduledAgent项目。 : 在此输入图像描述

Now my ScheduledAgent Class 现在我的ScheduledAgent类

#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) 这不是调用OnInvoke()方法(至少在调试模式下)

Note : I have added the Reference to ScheduledTaskAgent2 project also. 注意 :我还添加了对ScheduledTaskAgent2项目的引用。

Has anybody implemented the ScheduleAgent in WP 8.1 (Silverlight) 是否有人在WP 8.1(Silverlight)中实现了ScheduleAgent

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. 只需在Package.appxmanifest文件中添加此扩展。 you can open it by right click => viewcode option. 您可以通过right click => viewcode选项打开它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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