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?


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.


Use the Loaded event of the Window. You can configure this in the XAML like below:

<Window x:Class="WpfTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Your App" Loaded="Window_Loaded">

Here is what the Window_Loaded event would look like:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // do stuff
}