如何增加后台任务的运行时间?

How to increase the runtime of a background task?

所以我在 UWP 上有一个从服务器下载数据的后台任务。此任务最多可能需要一分钟,具体取决于互联网速度。我发现当我的用户最小化应用程序时下载暂停,所以决定 运行 它在 BackgroundTask

测试时我发现我的下载没有完成,后来从 this link 发现后台任务有限制:

Background tasks are limited to 30 seconds of wall-clock usage.

但经过更多谷歌搜索后,我发现 this link 谈到增加后台任务超时的方法

In the Universal Windows Platform, background tasks are processes that run in the background without any form of user interface. Background tasks may generally run for a maximum of twenty-five seconds before they are cancelled. Some of the longer-running tasks also have a check to ensure that the background task is not sitting idle or using memory. In the in the Windows Creators Update (version 1703), the extendedBackgroundTaskTime restricted capability was introduced to remove these limits. The extendedBackgroundTaskTime capability is added as a restricted capability in your app's manifest file:

Package.appxmanifest

XML

 <Package ...> 
    <Capabilities>  
        <rescap:Capability Name="extendedBackgroundTaskTime"/>      
    </Capabilities> 
  </Package> 

This capability removes execution time limitations and the idle task watchdog

但是将以上内容添加到我的 package.appxmanifest 文件后,出现以下错误:

Content of the file 'Package.appxmanifest' is not well-formed XML. 'rescap' is an undeclared prefix

是否可以从我的应用程序中删除此后台任务限制?

UWP 具有一项功能,允许您对下载进行排队,即使在应用程序关闭时也可以完成下载。 查看 this article,其中解释了如何使用此功能。

如果您仍想从后台任务开始下载,则让后台任务调用该功能,这样,当后台任务到期时,下载将继续并由 Windows 管理,并且UWP.

正如 Mike McCaughan 在对该问题的评论中所观察到的,对于您所看到的特定错误最可能的解释是您只是没有在封闭的 包上声明 rescap 命名空间 被能力标签引用的元素:

<Package
    xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
    xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
    xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="uap mp wincap rescap">

我不能说您是否需要其他命名空间,但您正在处理的受限功能案例中涉及的关键是 xmlns:rescap

信息取自documentation provided by Microsoft here