是否有确定的方法可以知道 UWP 视图渲染/绑定已完成?

问题描述

在 WPF 中,我可以使用调度程序将我的下一个 UI 相关代码(拍摄视觉效果的快照)排入队列,以便在所有待处理的 UI 操作完成后运行:

...code before rendering

Application.Current.dispatcher.BeginInvoke(dispatcherPriority.ApplicationIdle,() => 
{
...code to run after all prevIoUs UI operations have completed (snapshot of view)
});
...

有没有办法在 UWP 中做同样的事情?

解决方法

您可以将 CoreDispatcher.RunAsync API 与 CoreDispatcherPriorityLow 结合使用,以便在没有待处理的输入事件时调用委托:

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low,() => {
        ...
});

还有用于安排后台任务的 RunIdleAsync API:

await Dispatcher.RunIdleAsync(new IdleDispatchedHandler(x => {
    ...
}));