Software & Finance





Visual Studio.NET WPF - My First Application





Create a WPF C# application under VS 2010 Winzard with the name MyFirstWpfApp and then compile and run the code. No need to make any code change with the first application and try to understand the relationship between the Xaml and implemntation class and System.Windows.Application and System.Windows.Window classes.

 

Look at the following App.Xaml and App.Xaml.cs file. You should see a key link between the Xaml and its corresponding file. The key is MyFirstWpfApp.App defined in Xaml file refers to the namespace MyFirstWpfApp and the class name as App Which is derived from System.Windows.Application.

 

 

// App.Xaml File

 

<Application x:Class="MyFirstWpfApp.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             StartupUri="MainWindow.xaml">

    <Application.Resources>

        

    </Application.Resources>

</Application>

 

 

// App.Xaml.CS File

 

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Windows;

 

namespace MyFirstWpfApp

{

    /// <summary>

    /// Interaction logic for App.xaml

    /// </summary>

    public partial class App : Application

    {

    }

}

 

 

You may also notice two other files called MainWindow.Xaml and MainWindow.Xaml.cs. Again here also the key link is

MyFirstWpfApp.MainWindow Where MyFirstWpfApp is the namespace and MainWindow is the class name.

 

// MainWindow.Xaml File

 

<Window x:Class="MyFirstWpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">

    <Grid>

    </Grid>

</Window>

 

// MainWindow.Xaml.CS File

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace MyFirstWpfApp

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

    }

}

 

 

How MyFirstWpfApp.App connects with MyFirstWpfApp.MainWindow?

 

If you look at the source code generated by the VS 2010 wizard, you may not see any link between the Application and Window class. Surprising? Look at the word partial in the class implementation. It means the link is established through compiler generated temporary files located under obj\x86\debug\*.g.cs

 

The other partial implementation for MyFirstWpfApp.App generated by compiler has the link to MainWindow XAML file. This file also has the main function. Look at the following code. this.StartupUri does the magic.

 

namespace MyFirstWpfApp {

   

   

    /// <summary>

    /// App

    /// </summary>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]

    public partial class App : System.Windows.Application {

       

        /// <summary>

        /// InitializeComponent

        /// </summary>

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]

        public void InitializeComponent() {

           

            #line 4 "..\..\..\App.xaml"

            this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);

           

            #line default

            #line hidden

        }

       

        /// <summary>

        /// Application Entry Point.

        /// </summary>

        [System.STAThreadAttribute()]

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]

        public static void Main() {

            MyFirstWpfApp.App app = new MyFirstWpfApp.App();

            app.InitializeComponent();

            app.Run();

        }

    }

}