Each WPF application project created by Visual Studio using the default template consists of the following files:
- App.config: This is the configuration file of your WPF application. By default, it contains the following lines that describe the supported runtime version for the application to run. This contains exactly the same runtime version that we selected during the project creation:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime
version="v4.0"sku=".NETFramework,Version=v4.7" /> </startup> </configuration>
The config file can also contain application settings and other configuration settings that you want to use/refer in your application.
- App.xaml: Visual Studio automatically creates the App.xaml file when you create a WPF project. It is the declarative starting point of your application. The root element of this file is the Application instance, which defines application specific properties and events:
<Application x:Class="CH01.HelloWPFDemo.App"
xmlns="http://schemas.microsoft.com/winfx
/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CH01.HelloWPFDemo" StartupUri="MainWindow.xaml">
<Application.Resources> </Application.Resources> </Application>
The instance of the Application class defines the Window or a Page that's going to be the startup UI, and is registered with the StartupUri property. In the preceding code, (StartupUri="MainWindow.xaml") states that the MainWindow.xaml page will get loaded, once you run the application.
The application instance can also hold global/application-level resources (such as, Style, Template, and Converter) that can be used globally throughout the application.
- App.xaml.cs: This is the code-behind class file of the App.xaml and extends the Application class of the framework to write application-specific code. You can use this file to subscribe to the events such as Startup, UnhandledException to perform common operations:
namespace CH01.HelloWPFDemo { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { } }
This class is often used to manipulate command-line parameters and load different XAML pages based on that.
- MainWindow.xaml: This is the default UI page that Visual Studio generates on creation of the WPF project. It is the page that gets registered as the StartupUri in App.xaml. The root element of this page is Window and it contains a Grid layout by default. Here is the default code snippet:
<Window x:Class="CH01.HelloWPFDemo.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>
The x:Class attribute defines the associated partial class where the UI logic is being written. You can modify this XAML to provide a fresh look to your application start page. Various UI controls and layouts are going to be covered in the later chapters of this book.
- MainWindow.xaml.cs: This is the code-behind class of MainWindow.xaml and contains the logic related to UI operations. In general, developers write implementations of various UI operations in this class.
Whenever you add any UI elements to an XAML page, the control gets registered internally in a partial class file that has .g.i.cs as the extension. For example, if you add a control in the MainWindow.xaml file, it gets registered in the MainWindow.g.i.cs residing in the obj folder. If you open the file, you can observe the entire loading process inside the InitializeComponent() method.