如何知道 FrameworkElement 何时被完全渲染? [英] How to Know When a FrameworkElement Has Been Totally Rendered?

查看:22
本文介绍了如何知道 FrameworkElement 何时被完全渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 WPF,Window 类中有 ContentRendered 事件,它让我们知道何时呈现可视元素.

For WPF there is the ContentRendered event in the Window class which let us know when the visual elements have been rendered.

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

Is there anything that would help me achieve the same result for UWP apps? I'd like know when a FrameworkElement has been completely rendered so I can trigger some actions after that. I don't think the Loadedevent helps on that since it's triggered way before there's anything on screen.

推荐答案

我会从 Loaded 开始.它可能比你想象的要好.

I would start with Loaded. It might be better than you think.

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

Loaded 事件可用作连接来自模板的元素上的事件处理程序的点,或调用依赖于作为应用模板结果的子元素存在的逻辑.Loaded 是首选的对象生存期事件,用于在显示 UI 的 XAML 控件之前使用应用代码操作元素树结构.如果初始布局上没有发生其他事件(SizeChanged 确实发生在初始布局上),则从 Loaded 处理程序调用 VisualStateManager.GoToState 方法以设置模板中定义的初始视图状态也是合适的.

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.

Depending on your use case, consider LayoutUpdated.

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

LayoutUpdated 是在控件准备好交互之前在 XAML 加载序列中发生的最后一个对象生命周期事件.但是,LayoutUpdated 也可能在对象生命周期中的运行时发生,原因有多种:属性更改、窗口大小调整或运行时布局请求(UpdateLayout 或更改的控件模板).LayoutUpdated 事件在布局序列中的所有 SizeChanged 事件发生后触发.

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.

Also, there's SizeChanged you might consider.

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

SizeChanged 发生在页面上元素的初始布局期间,当应用程序第一次被激活时,因为 UI 元素的 ActualHeight 和 ActualWidth 值在布局发生之前是未定义的.它们仅在初始布局传递期间获取值,因此会发生 SizeChanged 事件.此后,在应用的生命周期内,如果 ActualHeight 和 ActualWidth 值因其他原因发生变化,则 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 事件中)向调度程序发布一个操作,该调度程序将等待执行,直到渲染完成.如果这是您想要的,请尝试此代码的变体:

Your question really didn't give me much to work with, but regardless of your use case, I bet this will come pretty close. That being said, it's also possible that you are trying to wait until the rendering is complete. A well known solution for this is to post (in the Loaded event of the control) an action to the dispatcher, which will wait to execute until after rendering is complete. If this is what you want, try a variant of this code:

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

祝你好运!

这篇关于如何知道 FrameworkElement 何时被完全渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆