如何知道 FrameworkElement 何时完全呈现?

How to Know When a FrameworkElement Has Been Totally Rendered?

对于 WPF,Window class 中有 ContentRendered 事件,它让我们知道视觉元素何时被渲染。

有什么可以帮助我为 UWP 应用程序实现相同的结果吗?我想知道 FrameworkElement 何时完全呈现,以便我可以在之后触发一些操作。我认为 Loaded 事件对此没有帮助,因为它是在屏幕上显示任何内容之前触发的。

以前在类似情况下这对我来说效果很好:

    ...
    await Dispatcher.RunAsync(CoreDispatcherPriority.Low, DoYourThing);
    ...

    private void DoYourThing()
    {

    }

我会从加载开始。它可能比你想象的要好。

https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.frameworkelement.loaded

The Loaded event can be used as a point to hook up event handlers on elements that come from a template, or to invoke logic that relies on the existence of child elements that are the result of an applied template. Loaded is the preferred object lifetime event for manipulating element tree structures with your app code prior to the display of XAML controls for your UI. It is also appropriate to call the VisualStateManager.GoToState method from a Loaded handler in order to set an initial view state that is defined in the template, if there's no other event that also occurs on initial layout (SizeChanged does occur on initial layout).

根据您的用例,考虑 LayoutUpdated。

https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.frameworkelement.layoutupdated

LayoutUpdated is the last object lifetime event to occur in the XAML load sequence before a control is ready for interaction. However, LayoutUpdated can also occur at run time during the object lifetime, for a variety of reasons: a property change, a window resizing, or a runtime layout request (UpdateLayout or a changed control template). The LayoutUpdated event is fired after all SizeChanged events in a layout sequence occur.

此外,您可以考虑使用 SizeChanged。

https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.frameworkelement.sizechanged

SizeChanged occurs during initial layout of elements on a page, when the app first is activated, because the ActualHeight and ActualWidth values for UI elements are undefined before layout happens. They only get values during the initial layout pass and thus the SizeChanged event occurs. Thereafter, during an app's lifetime, the SizeChanged event can fire from an element again if the ActualHeight and ActualWidth values change for other reasons.

你的问题确实没有给我太多帮助,但不管你的用例如何,我敢打赌这会非常接近。话虽如此,您也有可能试图等到渲染完成。一个众所周知的解决方案是 post(在控件的 Loaded 事件中)向调度程序执行一个操作,它将等待渲染完成后执行。如果这是您想要的,请尝试使用此代码的变体:

Dispatcher.Invoke(new Action(() => { }), DispatcherPriority.ContextIdle, null);

祝你好运!