C# Question

Does form.onload exist in WPF?

I would like to run some code onload of a form in WPF. Is it possible to do this? I am not able to find where to write code for form onload.

Judging by the responses below it seems like what I am asking is not something that is typically done in WPF? In Vb.Net winforms it is easy, you just go to the onload event and add the code that you need ran on load. For whatever reason, in C# WPF it seem very difficult or there is no standard way to do this. Can someone please tell me what is the best way to do this?

Answer Source

You can subscribe to the Window's Loaded event and do your work in the event handler:

public MyWindow()
  Loaded += MyWindow_Loaded;

private void MyWindow_Loaded(object sender, RoutedEventArgs e)
  // do work here

Alternatively, depending on your scenario, you may be able to do your work in OnInitialized instead. See the Loaded event docs for a discussion of the difference between the two.