我认为您可以像执行普通的 Windows Phone 8 后台任务一样实现此功能。我不知道有什么插件可以做到这一点,因为每个平台都以自己的方式处理这个......以this 为例。您需要修改应用清单文件并使用以下内容覆盖 WMAppManifest.xml 中的 DefaultTasks 元素:
<DefaultTask Name="_default" NavigationPage="MainPage.xaml">
<BackgroundExecution>
<ExecutionType Name="MyBackgroundThing" />
</BackgroundExecution>
</DefaultTask>
然后在 App.xaml 中,您需要覆盖 shell:PhoneApplicationService 元素来为 RunningInBackgroundEvent 注册事件处理程序,如下所示:
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"
RunningInBackground="Application_RunningInBackground"/>
然后,您需要在 App.xaml.cs 中声明一些对应用程序来说是全局的静态变量。这可能包括对后台服务的引用和 RunningInBackground 布尔值。像这样:
public static MyBackgroundService BackgroundService{ get; set; }
public static bool RunningInBackground { get; set; }
在 App.xaml.cs 中,添加 RunningInBackground 事件处理程序,如下所示:
private void Application_RunningInBackground(object sender, RunningInBackgroundEventArgs args)
{
RunningInBackground = true;
// Suspend all unnecessary processing such as UI updates
}
同时更新 Application_activated 方法,将 RunningInBackground 全局变量设置回 false,如下所示:
private void Application_Activated(object sender, ActivatedEventArgs e)
{
RunningInBackground = false;
}
取决于您的服务正在做的其他事情,他们可能还有其他事情要做,但这应该是首当其冲的......