Problems related to the two consecutive calls of the WPF Loaded event

Source: Internet
Author: User

In a recently developed system, when checking the code of the Development member, add the following statement in the Loaded event:

This. Loaded-= new RoutedEventHandler (*** _ Loaded); this makes me feel a bit strange. If you write a demo by referring to the Code logic in the system.

The MainWindow code is as follows:

Private void Window_Loaded (object sender, RoutedEventArgs e)
TabItem item = new TabItem ();
UserControl1 test = new UserControl1 ();
Item. Header = "test... ";
Item. Content = test;
Item. IsSelected = true;
Tbc. Items. Add (item );

UserControl1 loads another UserControl2. The front-end code is as follows, and there is no code in the background:

<Grid x: Name = "detailGrid">
<Grid. RowDefinitions>
<RowDefinition Height = "*"/>
</Grid. RowDefinitions>
<Grid. ColumnDefinitions>
<ColumnDefinition Width = "5 *"/>
<ColumnDefinition Width = "4"/>
<ColumnDefinition Width = "*"/>
</Grid. ColumnDefinitions>
<ScrollViewer Grid. Column = "2">
<My: UserControl2 Margin = "3"/>



The UserControl2 code is as follows:


<UserControl x: Class = "Two Loaded events. UserControl2"
Xmlns = ""
Xmlns: x = ""
Xmlns: mc = ""
Xmlns: d = "" Loaded = "UserControl_Loaded">

<Expander ExpandDirection = "Down" IsExpanded = "True" x: Name = "grpRunInfo">
<Expander. Header>
<TextBlock Text = "running information"/>
</Expander. Header>
<Expander. Content>
<ScrollViewer Name = "scrollViewer1" HorizontalScrollBarVisibility = "Auto" VerticalScrollBarVisibility = "Auto">
<Button Content = "second layer" Height = "60" Name = "button1" Width = "123"/>
</Expander. Content>



Public partial class UserControl2: UserControl
Public UserControl2 ()
InitializeComponent ();

Private void UserControl_Loaded (object sender, RoutedEventArgs e)
Console. WriteLine ("calling uc loaded ");
If (this. IsLoaded)
String s = string. Empty;
String B = string. Empty;

In this case, some simple code encountered a strange problem. The Loaded event of Usercontrol2 went twice and thought it was item. isSelected = true; this Code enables the Loaded event to run the second time. Otherwise, comment out the code and the Loaded event of Usercontrol2 is still run twice. This leads to a huge increase. None of them found the reason or the reason was incorrect when asking for help from their peers. Finally, I checked one by one and found that the ScrollViewer control of Usercontrol1 was faulty. column = "2"> comments on this layer, leaving only the <StackPanel> layer does not go through the UserControl2 Loaded event twice. My personal understanding is whether the ScrollViewer loads the control and the Loaded event of UserControl2 goes through the second time. I can only understand it in this way without being instructed by others. At this moment, I sent a demo to a buddy in the Group and replied, saying that I had not encountered any problems and I had to leave it for only once. This made me wonder, I thought it was because he didn't understand what I was talking about or that the problem I was talking about was not clearly described. Since I already know the reason, I don't care too much. I plan to talk to developers about the problem and how to modify it. At this time, another person who discussed previously replied and said that he had not encountered the problem I was talking about. He said that my ide had a problem and asked me to reinstall it. I said that I used vs2010, he said he used vs2012. Why? I also asked the previous person who said he hadn't encountered any problems. He also said he was using vs2012. I asked him if his compiling environment was used. net4.5, he said it was 4.0. I said that the 4.0 I used should not be a problem. If I downloaded a 2013 installation and testing, I also used it. net4.0; the same demo did not show the Loaded event twice. If you do not understand the reason, please give advice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.