In Universal Windows Platform (UWP) app development, it's common to display the application's version to users for various reasons, such as tracking updates or providing support. In this article, we will explore how to retrieve a project's version using C# and then display it in a label on the XAML side of the UWP app.

Prerequisites

To follow along with this tutorial, you'll need the following:

  1. Windows 10 with the latest updates.
  2. Visual Studio 2019 or later with UWP development workload installed.

Step 1: Retrieve the Project's Version in C#

  1. Open your UWP project in Visual Studio.
  2. Right-click on the project in the Solution Explorer and select "Manage NuGet Packages."
  3. In the NuGet Package Manager, search for and install the "Microsoft.Toolkit.Uwp" package. This toolkit provides helpful extensions for UWP development.
  4. Once the package is installed, open the C# file where you want to access the version information. Typically, you can use the MainPage.xaml.cs file.
  5. Add the following code to retrieve the project's version:
using Windows.ApplicationModel;

public string GetAppVersion()
{
    PackageVersion version = Package.Current.Id.Version;
    return $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
}

Step 2: Display the Version in XAML

  1. Open the XAML file where you want to display the version information. For this example, let's assume it's the MainPage.xaml file.
  2. In the XAML file, add a label to display the version information:
<Grid>
    <!-- Your other XAML elements go here -->

    <TextBlock x:Name="versionLabel"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               FontSize="20"
               Foreground="Black"
               Text="Version: " />
</Grid>

Step 3: Set the Label's Content from C#

  1. Switch back to the MainPage.xaml.cs file.