In this blog post I demonstrate how to use Visual Studio 2022 to create a new C++ Universal Windows Program (UWP) application.
I will then show you how I updated its XAML window to add a Button control and a TextBlock control to the window, hook the Button control to a Click event callback function, and then program that Click event callback function to update the text that is displayed by the TextBlock control whenever the Button is clicked on.
To begin, perform the following steps:
At this point you should now have a new blank C++ UWP application, which has one XAML window (i.e. MainWindow) already linked to it.
If you compile and run the program now, just to make sure it compiles and runs OK without errors, you should see a blank window:
Using Visual Studio 2022's editor, I edit the MainPage.xaml file of my UWP project and I add a StackPanel to the Grid, and then I add a Button and a TextBlock control to that StackPanel. Afterwards I edit their properties to change their size, color and font property values:
I then add a Click event to the Button control by typing Click="" into the Button element as an attribute, and then double clicking on the <New Event Handler> option from the displayed intelliSense popup context menu:
The front-end XAML source code:
<Page
x :Class ="Test1.MainPage"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns :x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns :local ="using:Test1"
xmlns :d ="http://schemas.microsoft.com/expression/blend/2008"
xmlns :mc ="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc :Ignorable ="d"
Background ="{ThemeResource ApplicationPageBackgroundThemeBrush }"
Width ="400" Height ="400" >
<Grid >
<StackPanel Margin ="10,10,0,0">
<Button x :Name ="Button1"
Width ="100"
Height ="40"
Content ="Click Me"
ClickMode ="Release"
Click ="Button1_Click">
</Button >
<TextBlock x :Name ="TextBlock1"
Foreground ="Maroon"
FontSize ="24"
FontWeight ="Bold">
</TextBlock >
</StackPanel >
</Grid >
</Page >
I then open the MainPage.xaml.cpp file and edit the source code to add some functionality to the Button's Click event callback function (Button1_Click). In this example, I simply instruct the program to update the TextBlock's displayed text whenever the Button control is clicked and re-clicked on:
The back-end C++ source code:
#include "pch.h"
#include "MainPage.xaml.h"
using namespace Test1;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
MainPage ::MainPage ()
{
InitializeComponent ();
}
void Test1::MainPage ::Button1_Click (Platform::Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e)
{
// respond to this button being clicked on
if (TextBlock1->Text != "Hello World!" )
{
TextBlock1->Text = "Hello World!" ;
}
else
{
TextBlock1->Text = "Click the button again." ;
}
}
After making the above modifications, now when I compile and run the program I see something similar to the following:
And each time I continue to click on the Click Me button it toggles the displayed text back and forth.
With the power of XAML combined with C++, the displayed controls on the window can be fully Styled in virtually any way you like, which gives your applications the familiarity of C#'s WPF but with the power and speed of C++.
Also, combining XAML with C++ might make it a little easier for C# WPF developers to migrate over into the C++ world.
Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.
About | Contact Us | Privacy | Terms & Conditions | © 2025 - T&J Divisions, LLC, All Rights Reserved |