Now that you have installed Visual Studio and created your first WPF project, it’s time to understand how a WPF application works. In this lesson, you’ll learn about the project structure, the MainWindow, how to build your application, and finally how to run it.
By the end of this tutorial, you’ll have your first WPF application running successfully.
Project Structure #
When you create a new WPF Application project in Visual Studio, several files and folders are automatically generated. Each file has a specific purpose.
A typical WPF project looks like this:
MyFirstWpfApp
│
├── App.xaml
├── App.xaml.cs
├── MainWindow.xaml
├── MainWindow.xaml.cs
├── MyFirstWpfApp.csproj
├── bin
├── obj
└── Properties
Let’s understand each of these files.
App.xaml #
App.xaml is the application’s entry point. It defines application-wide resources such as themes, styles, and the startup window.
Example:
<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>
The StartupUri property tells WPF which window should open when the application starts.
App.xaml.cs #
This file contains the application’s startup logic and lifecycle events.
Example:
public partial class App : Application
{
}
Most beginner applications don’t require changes to this file.
MainWindow.xaml #
This file contains the user interface (UI) of your application. WPF uses XAML (Extensible Application Markup Language) to define windows and controls.
Example:
<Window x:Class="MyFirstWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My First WPF App"
Height="300"
Width="500">
<Grid>
<TextBlock
Text="Hello WPF!"
FontSize="30"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</Window>
This window displays the text Hello WPF! in the center of the screen.
MainWindow.xaml.cs #
This is the code-behind file for MainWindow.xaml.
It contains C# code that interacts with the UI.
Example:
using System.Windows;
namespace MyFirstWpfApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
The InitializeComponent() method loads the XAML UI and creates all controls.
.csproj File #
The project file contains project settings such as:
- Target .NET version
- NuGet package references
- Build settings
- Application type
Example:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
You usually don’t need to edit this file manually.
bin Folder #
After building the project, Visual Studio creates the bin folder.
It contains:
- Executable (.exe)
- DLL files
- Dependencies
Example:
bin
└── Debug
└── net9.0-windows
MyFirstWpfApp.exe
obj Folder #
The obj folder stores temporary build files generated by Visual Studio.
These files are recreated automatically whenever you build the project.
MainWindow #
Every WPF application starts by displaying a window.
By default, Visual Studio creates a window called MainWindow.
Example XAML:
<Window x:Class="MyFirstWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window"
Height="350"
Width="600">
<Grid>
</Grid>
</Window>
The important properties are:
| Property | Description |
|---|---|
| Title | Window title displayed in the title bar |
| Width | Width of the window |
| Height | Height of the window |
| WindowState | Normal, Maximized, or Minimized |
| ResizeMode | Allows or prevents resizing |
| Icon | Window icon |
Example:
<Window
Title="Employee Management"
Width="900"
Height="600"
WindowStartupLocation="CenterScreen">
This opens a window centered on the screen.
Build the Application #
Building your application converts your C# and XAML files into an executable program.
There are several ways to build a WPF application.
Method 1: Build Menu #
Go to:
Build
↓
Build Solution
Shortcut:
Ctrl + Shift + B
What Happens During Build? #
Visual Studio performs several steps:
- Compiles C# code
- Compiles XAML
- Resolves references
- Creates executable (.exe)
- Copies required DLLs
XAML Tag Not Closed #
Incorrect:
<Button Content="Click">
Correct:
<Button Content="Click" />
Build Output #
The Output window displays messages such as:
========== Build: 1 succeeded, 0 failed ==========
Run the Application #
Running starts the compiled application.
Method 1 #
Click the green Start button.
Method 2 #
Press:
F5
This starts the application with the debugger attached.
Method 3 #
Press:
Ctrl + F5
This runs the application without the debugger.
Application Window #
When the application launches, you’ll see a window similar to this:
+--------------------------------------+
| My First WPF App □ X |
+--------------------------------------+
| |
| Hello WPF! |
| |
+--------------------------------------+
Congratulations! You’ve successfully built and run your first WPF application.
Stop the Application #
To stop the application:
- Close the window
- Or click Stop Debugging
- Or press:
Shift + F5
Build vs Run #
| Build | Run |
|---|---|
| Compiles the project | Starts the application |
| Checks for errors | Executes the program |
| Creates EXE | Opens the application window |
| Doesn’t launch the app | Launches the app |
Best Practices #
- Build frequently while coding.
- Fix compiler errors before continuing.
- Give meaningful names to windows and controls.
- Keep XAML clean and properly indented.
- Save your project regularly.
- Use
Ctrl + Shift + Bto verify your code compiles successfully.
Summary #
In this lesson, you learned:
- How a WPF project is organized.
- The purpose of
App.xamlandMainWindow. - How XAML and C# work together.
- How to build a WPF application.
- Different ways to run your application.
- The difference between building and running.
You now have a solid understanding of the basic structure of every WPF application. In the next tutorial, we’ll dive deeper into XAML syntax, where you’ll learn how to create user interfaces using layouts, controls, and properties.
