在 UWP 应用更新时运行后台任务Run a background task when your UWP app is updated

了解如何编写在通用 Windows 平台 (UWP) 应用商店应用更新后运行的后台任务。Learn how to write a background task that runs after your Universal Windows Platform (UWP) store app is updated.

在用户安装设备上安装的应用的更新后,操作系统调用 Update Task 后台任务。The Update Task background task is invoked by the operating system after the user installs an update to an app that is installed on the device. 这允许你的应用执行初始化任务,如初始化新的推送通知通道、更新数据库架构等,然后用户再启动你更新过的应用。This allows your app to perform initialization tasks such as initializing a new push notification channel, updating database schema, and so on, before the user launches your updated app.

Update Task 与使用 ServicingComplete 触发器启动后台任务不同,因为在后者中,你的应用在更新前必须运行至少一次来注册将用 ServicingComplete 触发器激活的后台任务。The Update Task differs from launching a background task using the ServicingComplete trigger because in that case your app must run at least once before it is updated in order to register the background task that will be activated by the ServicingComplete trigger. Update Task 未注册,因此从未运行但已升级的应用仍将触发其更新任务。The Update Task isn't registered and so an app that has never been run, but that is upgraded, will still have its update task triggered.

步骤 1:创建后台任务类Step 1: Create the background task class

与其他类型的后台任务相同,你以 Windows 运行时组件的形式实现 Update Task 后台任务。As with other types of background tasks, you implement the Update Task background task as a Windows Runtime component. 要创建此组件,请按照创建和注册进程外后台任务创建后台任务类部分中的步骤进行操作。To create this component, follow the steps in the Create the Background Task class section of Create and register an out-of-process background task. 步骤包括:The steps include:

  • 将 Windows 运行时组件项目添加到解决方案中。Adding a Windows Runtime component project to your solution.
  • 创建从你的应用到该组件的引用。Creating a reference from your app to the component.
  • 在实现 IBackgroundTask 的组件中创建一个公共的密封类。Creating a public, sealed class in the component that implements IBackgroundTask.
  • 实现 Run 方法,该方法是 Update Task 运行时调用的必需的入口点。Implementing the Run method, which is the required entry point that is called when the Update Task is run. 如果你要从后台任务进行异步调用,创建和注册进程外后台任务介绍了如何通过 Run 方法使用延迟。If you are going to make asynchronous calls from your background task, Create and register an out-of-process background task explains how to use a deferral in your Run method.

你无需注册此后台任务(创建和注册进程外后台任务主题的“注册要运行的后台任务”部分)即可使用 Update Task。You don't need to register this background task (the "Register the background task to run" section in the Create and register an out-of-process background task topic) to use the Update Task. 这是使用 Update Task 的主要原因,因为你无需在你的应用中添加任何代码来注册任务,应用也无需在更新前运行至少一次来注册后台任务。This is the main reason to use an Update Task because you don't need to add any code to your app to register the task and the app doesn't have to at least run once before being updated to register the background task.

下面的示例代码显示了 C# 中 Update Task 后台任务的基本起始点。The following sample code shows a basic starting point for a Update Task background task class in C#. 后台任务类本身以及后台任务项目中的所有其他类都必须为公共并且密封的。The background task class itself - and all other classes in the background task project - need to be public and sealed. 你的后台任务类必须派生自 IBackgroundTask 并且有一个具有如下签名的公共 Run() 方法:Your background task class must derive from IBackgroundTask and have a public Run() method with the signature shown below:

using Windows.ApplicationModel.Background;

namespace BackgroundTasks
{
    public sealed class UpdateTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // your app migration/update code here
        }
    }
}

步骤 2:在程序包清单中声明你的后台任务Step 2: Declare your background task in the package manifest

在 Visual Studio 解决方案资源管理器中,右键单击 Package.appxmanifest 并单击查看代码查看程序包清单。In the Visual Studio Solution Explorer, right-click Package.appxmanifest and click View Code to view the package manifest. 添加以下 <Extensions> XML 声明你的更新任务:Add the following <Extensions> XML to declare your update task:

<Package ...>
    ...
  <Applications>  
    <Application ...>  
        ...
      <Extensions>  
        <Extension Category="windows.updateTask"  EntryPoint="BackgroundTasks.UpdateTask">  
        </Extension>  
      </Extensions>

    </Application>  
  </Applications>  
</Package>

在上面的 XML 中,确保 EntryPoint 属性设置为你的更新任务类的 namespace.class 名称。In the XML above, ensure that the EntryPoint attribute is set to the namespace.class name of your update task class. 该名称区分大小写。The name is case-sensitive.

步骤 3:调试/测试你的更新任务Step 3: Debug/test your Update task

确保你已将应用部署到你的计算机以便进行更新。Ensure that you have deployed your app to your machine so that there is something to update.

在你的后台任务的 Run() 方法中设置一个断点。Set a breakpoint in the Run() method of your background task.

设置断点

接下来,在解决方案资源管理器中右键单击你的应用的项目(不是后台任务项目),然后单击属性Next, in the solution explorer, right-click your app's project (not the background task project) and then click Properties. 在应用程序“属性”窗口中,单击左侧的调试,然后选择不启动,但在启动时调试代码In the application Properties window, click Debug on the left, then select Do not launch, but debug my code when it starts:

设置调试设置

接下来,为确保 UpdateTask 触发,请增加程序包的版本号。Next, to ensure that the UpdateTask is triggered, increase the package's version number. 在解决方案资源管理器中,双击应用的 Package.appxmanifest 文件以打开程序包设计器,然后更新版本号:In the Solution Explorer, double-click your app's Package.appxmanifest file to open the package designer, and then update the Build number:

更新版本

现在,在 Visual Studio 2019 中按 F5 时,将更新应用程序,系统将在后台激活你的 UpdateTask 组件。Now, in Visual Studio 2019 when you press F5, your app will be updated and the system will activate your UpdateTask component in the background. 调试此程序将自动连接到后台进程。The debugger will automatically attach to the background process. 将命中你的断点,你可以单步执行更新代码逻辑。Your breakpoint will get hit and you can step through your update code logic.

后台任务完成时,你可以在同一调试会话中从 Windows“开始”菜单启动前台应用。When the background task completes, you can launch the foreground app from the Windows start menu within the same debug session. 调试程序将再次自动连接,这此连接到你的前台进程,你可以单步执行应用的逻辑。The debugger will again automatically attach, this time to your foreground process, and you can step through your app's logic.

备注

Visual Studio 2015 用户:以上步骤适用于 Visual Studio 2017 或 Visual Studio 2019。Visual Studio 2015 users: The above steps apply to Visual Studio 2017 or Visual Studio 2019. 如果你使用 Visual Studio 2015,则可以使用相同的技术触发和测试 UpdateTask,除非 Visual Studio 无法连接到它。If you are using Visual Studio 2015, you can use the same techniques to trigger and test the UpdateTask, except Visual Studio will not attach to it. VS 2015 中的替代过程是设置 ApplicationTrigger,它将 UpdateTask 设置为其入口点,并从前台应用直接触发执行。An alternative procedure in VS 2015 is to setup an ApplicationTrigger that sets the UpdateTask as its Entry Point, and trigger the execution directly from the foreground app.

另请参阅See also

创建和注册进程外后台任务Create and register an out-of-process background task