gpt4 book ai didi

c# - 如何知道 FrameworkElement 何时完全呈现?

转载 作者:太空狗 更新时间:2023-10-29 20:58:25 28 4
gpt4 key购买 nike

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

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

最佳答案

我将从 Loaded 开始。它可能比您想象的要好。

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.

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

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

祝你好运!

关于c# - 如何知道 FrameworkElement 何时完全呈现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340134/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com