Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Custom Control Lifecycle Diagram?

Tags:

I am looking for a diagram that will serve as a reference to show the order of events and overrides that get called for WPF custom controls that derive from common classes such as FrameworkElement, UIElement, Control, ContentControl, Decorator, etc.

I am particularly interested in the events and overrides dealing with the process of the control becoming visible and ready to interact with. In other words, I'm not as interested in this diagram showing the events that could deal with the user interacting with the control after it is ready to be interacted with, such as things like MouseLeftButtonDown.

What I am looking for is something like this diagram (diagrams like these explain the lifecyle of a WPF window or application) but for custom controls.

I've also seen this page, which, in my opinion makes it seem too simplistic. That page seems to basically say that there's mostly just the Initialized, Loaded, and Unloaded events. To illustrate, a few of the general events/overrides I've needed to make use of in custom controls include:

  • Constructor
  • Loaded
  • UnLoaded
  • IsVisibleChanged
  • OnApplyTemplate

Along with slightly more specialized overrides like Popup's OnOpened and Expander's OnExpanded, etc.

The problem I am having is that I forget when I can do things like VisualStateManager.GoToState(...) (OnApplyTemplate is the first real chance I believe). And I also keep discovering events that I need to make use of, such as the IsVisibleChanged (this event proved necessary when I needed a control to do a "reload/refresh" state change when the user returns to the Tab that contains my control). So I keep wondering are there yet other such events/overrides that I should really be paying attention to for a custom control? This is where a diagram that shows these things in relation to each other will help.

Considering these types of events/overrides, this Silverlight chart is pretty close to what I'm looking for, but for WPF. Some of the commenters on that post say that WPF's events/overrides are different than Silverlight's. If so, can you please point me to a similar diagram?

To further illustrate the need for such a diagram, consider the following sequence of events that I just witnessed on a custom control that derives from ContentControl:

  1. App started up. (My custom control is in a different tab than the intial tab.)
  2. Initialized event callback called
  3. Loaded event callback called
  4. (I click on the tab that contains my custom control)
  5. IsVisibleChanged event callback called
  6. OnApplyTemplate override called
  7. Loaded event callback called
  8. Loaded event callback called again
  9. Loaded event callback called third time in a row
  10. (Control is now fully visible and ready to interact with)
like image 723
Jason Frank Avatar asked Feb 07 '13 22:02

Jason Frank


2 Answers

I doubt such a comprehensive diagram exists. How about you just make it yourself? Override all the methods and/or events your are interested in and put a Trace.WriteLine(new StackFrame(1).GetMethod().Name); in each override. Your output will tell you in which order they were called.

like image 144
bitbonk Avatar answered Oct 08 '22 05:10

bitbonk


  1. Initialised is called only once and first
  2. OnApplyTemplate is called second, and whenever the template changes
  3. Loaded is called when the control is ready to be displayed and whenever certain containers reload their contents (notably swapping between TabItems)
  4. IsVisibleChanged is called whenever the visibility changes, and after loaded

Unloaded is not normally called unless you are in a TabItem and you swap to another one.

The loaded/unloaded events are not called when you would expect unfortunatly, and this makes it tricky to tell when your control should dispose of it's resources. I have also never managed to find a list of the containers that unload and reload their contents.

like image 39
stuart wilson Avatar answered Oct 08 '22 05:10

stuart wilson