wpf - Odd Loaded behavior in markup versus code -


I am currently very confused with the different behavior about the FrameworkElement.Loaded event. There is a small example app that shows it.

Xaml:

  & lt; Window x: orbit = "wpfApplication1.Window1" xmlns = "http: /sschems.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "title =" window 1 "height =" 300 "width =" 300 "loaded =" window_loaded "& gt; & Lt; Grid & gt; & Lt; TabControl & gt; & Lt; TabItem Header = "Tab 1" / & gt; & Lt; TabItem Header = "Tab2" & gt; & Lt; WindowsFormsHost name = "formhost" loaded = "form host_loaded" /> & Lt; / TabItem & gt; & Lt; / TabControl & gt; & Lt; / Grid & gt; & Lt; / Window & gt;  

Code:

  using System.Windows; Namespace WpfApplication1 {Public partial squares window 1: window {public window 1 () {InitializeComponent (); } Private Zero Window_loaded (Object Sender, Routing Avenger EG E) {formHost.Loaded + = delegate {MessageBox.Show ("Representative"); }; } Private Zero formhost_loaded (Object Sender, Routing Avenger AGE E) {Message Box. Show ("markup"); }}}  

As soon as I run the application, I get two instant messagebox es - "markup" and "representative". If, however, I delete Loaded = "formHost_Loaded" to WindowsFormsHost , I do not get it at startup, it clearly shows that I have a "markup" dialog Why not get it, but why does it remove "representative"? I think that in its sequence, the events that are called (window versus their children) have to do this, but I find it difficult to find out.

Note: You can replace WindowsFormsHost with other controls, it should not really make any difference - I was just using it for some more tests.

Loaded event handlers are defined before any name, because loaded events are fully Once on the top of the pad starts broadcasting effectively to everyone. WPF has decided which handler should be called, after which you add a handler, it is ignored.

This can be verified using the reflector. In particular, the BroadcastEventHelper.BroadcastLoadedSynchronously method will call the BroadedEventHelper.BroadcastEvent method with the LoadedEvent routed event.

The BroadcastEvent method collects all objects in the visual tree, in which the loaded event handler is first, then the loops and the incidents of those items picks up.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -