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.

<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.

        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();
                }
        }
Advertisements
This entry was posted in .NET, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s