UWP Development, Basics of XAML Programming

UWP (Universal Windows Platform) is an application platform developed by Microsoft that provides the foundation for developing applications that can run on all devices with Windows 10 and later versions.
UWP apps allow for the reuse of the same code across various devices, providing users with a consistent experience.

1. Setting Up the UWP Development Environment

To develop UWP applications, you need to install Visual Studio on a Windows 10 environment.
Install the latest version of Visual Studio and select the “Developer workload” for UWP development.

1.1 Steps to Install Visual Studio

  1. Visit the Visual Studio website and download the installer.
  2. Run the installation and select “Universal Windows Platform development” from the “Developer workload”.
  3. Choose the necessary components and complete the installation.

2. Understanding XAML

XAML (eXtensible Application Markup Language) is a markup language used to design the UI of UWP applications.
XAML is XML-based and allows for the declarative definition of UI elements.

2.1 Basic Structure of XAML

A XAML file typically has the following structure:

<Page
    x:Class="MyApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <TextBlock Text="Hello, UWP!" FontSize="24" />
    </Grid>
</Page>

The above XAML code defines a basic page and a text block.
‘Grid’ serves as a container for placing UI elements, while ‘TextBlock’ is an element that simply displays text.

3. Basic Components of UWP Applications

UWP applications consist of various components. Here, we describe the main components: pages, user controls, and resources.

3.1 Page

Each screen of a UWP application is represented by a page. Each page can be loaded independently and navigated to via a URL.
Typically, `Frame` is used to handle navigation between pages.

Frame.Navigate(typeof(SecondPage));

The above code demonstrates how to navigate from the current page to the SecondPage.

3.2 User Control

User controls are reusable UI elements. In large-scale applications, it is advisable to manage the UI by splitting it into multiple user controls.

<UserControl x:Class="MyApp.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid>
        <TextBlock Text="This is a custom control." />
    </Grid>
</UserControl>

3.3 Resources

In UWP, you can define resources for commonly used values, styles, or brushes across multiple UI elements.
Resources can be defined in Application.Resources for global access.

<Application.Resources>
    <SolidColorBrush x:Key="PrimaryColor" Color="Blue"/>
</Application.Resources>

4. Events and Data Binding

In XAML, you can connect UI elements with events and dynamically update the UI based on data.

4.1 Event Handling

<Button Content="Click Me" Click="Button_Click"/>

This is how you can handle a button click event.

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Event handling code
}

4.2 Data Binding

Data binding allows you to easily synchronize between the UI and the data model.
You can set the data context in the XAML line and bind to the properties of the UI elements.

<TextBlock Text="{Binding Name}"/>

The above code defines a TextBlock that is bound to the ‘Name’ property.
Here’s how to set the data context.

this.DataContext = this;

5. Deployment of UWP Apps

After developing a UWP application, you can deploy it to the Microsoft Store.
To deploy, you must create an app package and submit it for review by the deadline.

5.1 Creating an App Package

Package creation can be done through the “Project” > “Create App Package” menu in Visual Studio.
Select the platform for deployment (Windows 10, ARM, etc.) and follow the step-by-step instructions to create the package.

5.2 Submitting to Microsoft Store

After creating the package, you must submit the application through the Microsoft Partner Center and undergo a review.
During submission, you need to provide the app’s screenshots, description, and other metadata.

Conclusion

We covered the basic understanding of UWP development and XAML. UWP offers many advantages for developing efficient and effective applications.
The structure of XAML, event handling, data binding, and other elements are essential for UWP development.
Based on the content introduced in this course, try your hand at developing full-fledged UWP applications.

Sample Code

Below is a complete code example of a simple UWP application.

<Page
    x:Class="MyApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid>
        <TextBlock Text="{Binding Greeting}" FontSize="24" />
        <Button Content="Click Me" Click="Button_Click" />
    </Grid>
</Page>
public sealed partial class MainPage : Page
{
    public string Greeting { get; set; } = "Hello, UWP!";

    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Greeting = "Button Clicked!";
        OnPropertyChanged(nameof(Greeting));
    }
}