Create Your First Windows App
Welcome to your first step in building powerful applications for the Windows platform. This guide will walk you through the essentials of setting up your development environment and creating a simple "Hello, World!" application.
1. Setting Up Your Development Environment
Before you can start coding, you need the right tools. The primary tool for Windows development is Visual Studio.
Installing Visual Studio
- Download Visual Studio Community Edition (it's free!) from the official Visual Studio website.
- Run the installer and select the "Universal Windows Platform development" workload. This will include the necessary SDKs and components.
- Complete the installation.
Note: Ensure you have enough disk space. The UWP development workload can be several gigabytes.
2. Creating Your First Project
Now, let's create a new project in Visual Studio.
- Open Visual Studio.
- Click on "Create a new project".
- In the search bar, type "Windows App" and select "Blank App (Universal Windows)".
- Click "Next".
- Give your project a name (e.g., "MyFirstUWPApp") and choose a location.
- Click "Create".
3. Understanding the Project Structure
Upon project creation, you'll see a few key files and folders:
App.xaml&App.xaml.cs: Application-level definitions and startup logic.MainPage.xaml&MainPage.xaml.cs: The UI and code-behind for your main application page.Package.appxmanifest: Defines app metadata, capabilities, and other configurations.
4. Designing the User Interface (XAML)
The UI is defined using XAML (eXtensible Application Markup Language). Open MainPage.xaml. Visual Studio provides a design surface and a XAML editor.
Let's add a `TextBlock` to display "Hello, Windows!". Replace the default content of MainPage.xaml with the following:
<Page
x:Class="MyFirstUWPApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyFirstUWPApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<TextBlock Text="Hello, Windows!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="48"
Foreground="{ThemeResource SystemAccentColor}"/>
</Grid>
</Page>
Tip: The {ThemeResource SystemAccentColor} binding will automatically use the user's chosen accent color in Windows.
5. Adding Interactivity (C# Code-Behind)
While our current app is static, most apps require dynamic behavior. This is handled in the code-behind file, MainPage.xaml.cs.
Let's add a button that changes the text when clicked. First, modify MainPage.xaml to include a button:
<Page
x:Class="MyFirstUWPApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyFirstUWPApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="20">
<TextBlock x:Name="GreetingTextBlock"
Text="Hello, Windows!"
FontSize="48"
Foreground="{ThemeResource SystemAccentColor}"
HorizontalAlignment="Center"/>
<Button Content="Change Greeting"
HorizontalAlignment="Center"
Click="ChangeGreetingButton_Click"/>
</StackPanel>
</Grid>
</Page>
Now, open MainPage.xaml.cs and add the event handler for the button click:
using Windows.UI.Xaml.Controls;
namespace MyFirstUWPApp
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void ChangeGreetingButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
GreetingTextBlock.Text = "Hello, Developer!";
}
}
}
6. Running Your Application
To run your app:
- In the Visual Studio toolbar, select "Local Machine" from the dropdown.
- Click the "Start" button (the green triangle).
Your application will build and launch, allowing you to test your "Hello, World!" app. Click the button to see the text change!
Note: You can also deploy to emulators or physical devices for testing.
Next Steps
Congratulations on creating your first Windows app! From here, you can explore:
- Adding more UI controls like text boxes, images, and lists.
- Learning about layout panels (StackPanel, Grid, RelativePanel).
- Handling user input and navigation.
- Accessing device capabilities and Windows features.
Continue exploring the MSDN Windows Documentation for more in-depth guides and API references.