How to Know When a FrameworkElement Has Been Totally Rendered?

后端 未结 2 565
醉话见心
醉话见心 2021-01-02 08:18

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

Is there anyt

相关标签:
2条回答
  • 2021-01-02 08:33

    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

    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).

    Depending on your use case, consider 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.

    Also, there's SizeChanged you might consider.

    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.

    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);
    

    Best of luck!

    0 讨论(0)
  • 2021-01-02 08:41

    This worked well for me before in similar scenarios:

        ...
        await Dispatcher.RunAsync(CoreDispatcherPriority.Low, DoYourThing);
        ...
    
        private void DoYourThing()
        {
    
        }
    
    0 讨论(0)
提交回复
热议问题