MSDN Windows Tutorials

UWP Basics: Building Your First App

Welcome to the Universal Windows Platform (UWP) basics guide. In this tutorial, you'll learn the fundamental concepts of building UWP applications for Windows 10 and Windows 11. UWP allows you to create a single app that runs across various Windows devices, from PCs and tablets to Xbox and HoloLens.

What is UWP?

The Universal Windows Platform (UWP) is an application platform introduced by Microsoft with Windows 10. It provides a consistent set of APIs that allow developers to build apps that can run on any Windows device. Key features include:

Setting Up Your Development Environment

Before you begin, ensure you have the necessary tools installed:

  1. Visual Studio: Download and install the latest version of Visual Studio from the official Visual Studio website. Make sure to include the "Universal Windows Platform development" workload during installation.
  2. Windows SDK: The UWP development workload typically includes the necessary Windows SDKs.

Creating Your First UWP Project

Let's create a simple "Hello, World!" application.

  1. Open Visual Studio.
  2. Click "Create a new project".
  3. Search for "Blank App (Universal Windows)".
  4. Select it and click "Next".
  5. Name your project "HelloWorldUWP" and choose a location. Click "Create".
  6. Visual Studio will generate the basic project structure.

Understanding the Project Structure

Your UWP project will have several key files:

Designing the UI with XAML

Open MainPage.xaml. XAML (Extensible Application Markup Language) is used to define the user interface. Here's a basic example:

<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="HelloWorldUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HelloWorldUWP"
    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">
            <TextBlock Text="Hello, UWP World!" FontSize="36" FontWeight="SemiBold" Foreground="SteelBlue" />
            <Button x:Name="ClickMeButton" Content="Click Me" Margin="0,20,0,0" HorizontalAlignment="Center" Click="ClickMeButton_Click"/>
        </StackPanel>
    </Grid>
</Page>

Adding Interaction with C#

Now, let's add some functionality. Open MainPage.xaml.cs and add the following event handler for the button:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace HelloWorldUWP
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void ClickMeButton_Click(object sender, RoutedEventArgs e)
        {
            // You can change the text of the TextBlock or display a message dialog
            var textBlock = (TextBlock)this.FindName("HelloTextBlock"); // Assuming you named the TextBlock "HelloTextBlock"
            if (textBlock != null)
            {
                textBlock.Text = "Button Clicked!";
            }
            else
            {
                // If TextBlock is not found, display a message dialog (requires more setup)
                // For simplicity, we'll assume the TextBlock is present and correctly named
            }
        }
    }
}

Note: To make the C# code above work, you'd need to name the TextBlock element in XAML, for example: <TextBlock x:Name="HelloTextBlock" Text="Hello, UWP World!" ... />.

Running Your Application

Press F5 or click the "Local Machine" button in Visual Studio's toolbar to build and run your application. You should see your "Hello, UWP World!" app appear, and clicking the button will change the text.

Common UWP Controls

UWP provides a rich set of UI controls. Here are a few common ones:

Next Steps

This was a very basic introduction. To continue your UWP journey, explore topics such as:

Refer to the official Microsoft UWP Documentation for in-depth guides and API references.

← Previous Tutorial Next Tutorial →