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

查看:45
本文介绍了如何知道何时完全渲染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事件可以用作挂接事件处理程序到来自模板的元素上的点,或调用依赖于所应用模板的结果是否存在子元素的逻辑.加载是首选对象生命周期事件,用于在显示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或更改的控件模板).在布局序列中的所有SizeChanged事件发生之后,将触发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.

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事件中(在控件的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天全站免登陆