Saturday, September 3, 2011

The Initialized event and Loaded event in WPF

WPF controls has both Initialize and Loaded events. Initializing and loading a control tend to happen at about the same time, and consequently these events fire at roughly the same time.

The Initialized event says just that an element has been created and its properties have all been set, and as a consequence this usually fires on children before their parent. So when Initialized is raised on an element, its whole sub-tree is likely initialized, but its parent is not. The Initialized event is typically fired when the Xaml for a sub-tree is loaded. This event corresponds to the IsInitialized property.

The Loaded event says that the tree is not only built and initialized, but layout has run on it, data has been bound, it's connected to a rendering surface (window), and you're on the verge of being rendered. When we reach that point, we canvas the tree by broadcasting the Loaded event, starting at the root. This event corresponds to the IsLoaded property.



Example: in case of a TextBox inside a Page:

  • TextBox.IsInitialized goes true
  • TextBox.Initialized event is raised
  • Page.IsInitialized goes true
  • Page.Initialized event is raised
  • Page IsLoaded goes to true
  • TextBox IsLoaded goes to true
  • Page.Loaded is raised
  • TextBox.Loaded is raised

No comments:

Post a Comment

If you’re using Visual Studio to build your .NET MAUI app on a Mac, locating the IPA (iOS App Package) file can be a bit tricky

  If you’re using   Visual Studio   to build your   .NET MAUI   app on a   Mac , locating the   IPA (iOS App Package)   file can be a bit tr...