如何获取 IBackgroundTaskInstance?

How to get IBackgroundTaskInstance?

我需要它以便在需要时为当前任务执行 XamlRenderingBackgroundTask.OnRun . But I'm actually asking the more general question here - how does one get the IBackgroundTaskInstance

我认为您严重误解了 OnRun 方法的用途。正如 the linked documentation 所述:

The system calls this method when the associated background task has been triggered.

它是作为基础结构的一部分提供的 protected 方法。因此,它由基础架构代码自动调用,您(客户端)仅应出于处理目的覆盖它。

你基本上从不自己调用 OnRun,因此你永远不需要提供它的任何参数。相反,您 传递了 对实现 IBackgroundTaskInstance 的对象的引用,当函数被调用时,您可以在派生 [=24] 中的方法主体的实现中使用它=].