简体   繁体   English

Windows Phone Silverlight 8.1后台代理

[英]Windows Phone Silverlight 8.1 background agent

I am developing a Silverlight 8.1 app in c#. 我正在用C#开发Silverlight 8.1应用程序。 I want to use some of the old phone APIs inside my background periodic task, so I can't use the new IBackgroundTask RT interface. 我想在后台定期任务中使用一些旧的电话API,因此无法使用新的IBackgroundTask RT接口。

I created an old style Background agent with OnInvoke override then registered as before in WMAppManifest.xml: <ExtendedTask Name="BackgroundTask"> <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="BackgroundAgent" Source="BackgroundAgent" Type="BackgroundAgent.ScheduledAgent" /> </ExtendedTask> 我创建了一个具有OnInvoke覆盖的旧式背景代理,然后像以前一样在WMAppManifest.xml中进行了注册: <ExtendedTask Name="BackgroundTask"> <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="BackgroundAgent" Source="BackgroundAgent" Type="BackgroundAgent.ScheduledAgent" /> </ExtendedTask>

Where BackgroundAgent is my background agent library and ScheduledAgent is my class. 其中BackgroundAgent是我的背景代理程序库,而ScheduledAgent是我的类。

I have enabled Toast notifications for the app in the Package.appxmanifest. 我已在Package.appxmanifest中为该应用程序启用Toast通知。 My notification system is WNS (in WMAppManifest). 我的通知系统是WNS(在WMAppManifest中)。

I am trying to launch the task in the old school way: 我正在尝试以旧派方式启动任务:

BackgroundExecutionManager.RequestAccessAsync();
  var periodicTask = ScheduledActionService.Find("BackgroundTask");

  if (periodicTask != null)
  {
    ScheduledActionService.Remove("BackgroundTask");
  }

  periodicTask = new PeriodicTask("BackgroundTask");
  (periodicTask as ScheduledTask).Description = "Hello, world.";
  ScheduledActionService.Add(periodicTask);

  ScheduledActionService.LaunchForTest("BackgroundTask", TimeSpan.FromSeconds(60));

Inside the OnNavigatedTo method of my MainPage.xaml.cs. 在MainPage.xaml.cs的OnNavigatedTo方法内。

I can see this code run. 我可以看到这段代码正在运行。

However, I never see any code run inside my background agent - it is supposed to send me a toast (I tried both with the ShellToast and the new ToastNotificationManager for xml-based toasts) and make a http call - both of which don't happen. 但是,我从来没有看到任何代码在我的后台代理中运行-它应该向我发送敬酒(我对基于XML的敬酒尝试了ShellToast和新的ToastNotificationManager)并进行了http调用-两者都不发生。

I watched the Build™ video where they mentioned that background agents should be fully supported in Silverlight 8.1 apps. 我观看了Build™视频,他们提到在Silverlight 8.1应用程序中应该完全支持后台代理。

I can also confirm that my app appears as 'allowed' inside the battery Saver settings, which indicates that the background agent was registered with the os. 我还可以确认我的应用在Battery Saver设置中显示为“允许”,这表明后台代理已在os中注册。

What am I doing wrong? 我究竟做错了什么?

I think you need to have a background task of type "System event" with entry point "AgHost.BackgroundTask" in the Package.appxmanifest. 我认为您需要在Package.appxmanifest中具有“系统事件”类型的后台任务,入口点为“ AgHost.BackgroundTask”。 That's what's hosting the SL background agent in WP8.1 SL apps. 这就是在WP8.1 SL应用程序中托管SL后台代理的原因。 If you don't have this task in the manifest file, add it yourself and see it fixes your problem. 如果清单文件中没有此任务,请自己添加它,然后查看它可以解决您的问题。

Update 更新

From my experience the "System event" type should work, but zaitsman says that he needed to use "Timer", so if the first one does not work for you - try the other. 根据我的经验,“系统事件”类型应该可以工作,但是zaitsman说他需要使用“计时器”,因此,如果第一个对您不起作用,请尝试另一个。

Update 2 更新2

In other words, you need to have this: 换句话说,您需要具备以下条件:

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

inside the Application tag of the Package.appxmanifest file (not to be confused with WMAppManifest.xml) of your WP 8.1 SL app. WP 8.1 SL应用程序的Package.appxmanifest文件的Application标签(不要与WMAppManifest.xml混淆)中。

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

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