Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window.loaded fires before the window is fully loaded

Tags:

wpf

I have this code:
Window w = // something
w.Loaded += // some code to take a screenshot of the window
w.Show();

The Loaded event fires before the window is fully loaded and I get this image: Window screenshot

I could add a Thread.Sleep or something after w.Show() and before the screenshot but I need to run this piece of code for hundreds of windows.
My question is: Is there another event that fires when the window is fully loaded? or some way to achieve this without putting the thread to sleep.

Thanks

like image 398
Orlando William Avatar asked Jan 16 '12 21:01

Orlando William


1 Answers

Looking into Object Lifetime Events article in MSDN you can find:

The Loaded event is raised before the final rendering, but after the layout system has calculated all necessary values for rendering. Loaded entails that the logical tree that an element is contained within is complete, and connects to a presentation source that provides the HWND and the rendering surface.

You should try some alternative events which are not exactly suited for your need. You should try in following order:

  • Window.ContentRendered
  • UIElement.LayoutUpdated
  • Window.Activated

Take note that these events may fire multiple times during lifetime of your window, so write your application with that in mind.

WPF is data driven UI architecture and it's not very pleasant to work with in a way we are used to in WinForms. As someone smarter than me once said, WPF makes hard things trivial and trivial things hard.

like image 118
Nikola Radosavljević Avatar answered Sep 19 '22 08:09

Nikola Radosavljević