C# WPF - customizing start up fuction in WPF
When you create a WPF application with visual studio, you will always find app.xaml and app.xaml.cs file.
In app.xaml file, you can find the StartupUri attribute in Application tag. The StartupUri will indicate where the application starts. However sometimes you want to do some initialization before starting up the application then you need to modify the start up point with the following changes. As it shows you can specify the function name on start up point.
1
2
3
4
5
6
<Application x:Class="WpfTutorialSamples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup">
<Application.Resources></Application.Resources>
</Application>
After modifying the xaml, your application class should include the "Application_Startup" function as shown the below code.
1
2
3
4
5
6
7
8
9
10
11
12
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
// Create the startup window
MainWindow wnd = new MainWindow();
// Do stuff here, e.g. to the window
wnd.Title = "Something else";
// Show the window
wnd.Show();
}
}