Does form.onload exist in WPF?

C#Wpf

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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
}

Solution 3 - C#

This question was asked 4 years ago, but this answer may help others too so here goes --> To do it simply and quickly - down and dirty, put the code you want to run in a method in the code-behind. then simply call the method before the MainWindow() InitializeComponent(). This poses dangers, but most times it works because the components are loaded before window initiation/display. (This is working code from one of my projects.) Let's say you want to play a short wave file when the app fires up. It would look like this;

using ...
using System.Windows.Media;

namespace yourNamespace_Name
{
    /// sumary >
    /// Interaction logic for MainWindow.xaml
    /// /sumary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            /*call your pre-written method w/ all the code you  wish to
             * run on project load. It is wise to set the method access
             * modifier to 'private' so as to minimize security risks.*/
            playTada();

            InitializeComponent();
        }

        private void playTada()
        {
            var player = new System.Media.SoundPlayer();
            player.Stream = Properties.Resources.tada;
            // add the waveFile to resources, the easiest way is to copy the file to
            // the desktop, resize the IDE window so the file is visible, right 
            // click the Project in the solution explorer & select properties, click
            // the resources tab, & drag and drop the wave file into the resources 
            // window. Then just reference it in the method.
            // for example: "player.Stream = Properties.Resources.tada;"         
            player.Play();
            //add garbage collection before initialization of main window
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}

Hope this helps those that are searching. :-)

Solution 4 - C#

Loaded event is raised after project is build. To do stuff before, you can ovveride OnStartup method in App.xaml.cs.

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        //...
        base.OnStartup(e);
    }
}  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex GordonView Question on Stackoverflow
Solution 1 - C#itowlsonView Answer on Stackoverflow
Solution 2 - C#Taylor LeeseView Answer on Stackoverflow
Solution 3 - C#error 418View Answer on Stackoverflow
Solution 4 - C#R. PelloView Answer on Stackoverflow