summary:
- There are some actions that must be executed when a UserControl is created.
- Is it a bad to declare this to the constructor?
question:
- I always declared some actions to the constructor when creating Usercontrol. As below.
public MainWindow()
{
InitializeComponent();
InitSomething();
}
- My boss advised me, "If you write it like this, some problem will occur, so I should write it as follows."
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
InitSomething();
this.Loaded -= OnLoaded;
}
- I asked him what some problem was with him, but he said he had forgotten (...). But he said this is a famous issue and it will be easy to find through search.
- I found the following article through the search.
should-i-use-the-constructor-the-initialized-or-the-loaded-event
WPF: What is between the Initialized and Loaded event?
- Even though I read these articles, I couldn't figure out "why I shouldn't do InitSomething on the constructor", or "what kind of some problem are".
- I searched for about 2 hours, but I couldn't find related data because I didn't have enough ability to search. I ask for your help.
- I am not an English user, so I received help from a translator to write this article. Thank you very much for reading the poor article until the end.