Hello World
Learn how to output Hello World using Windows App SDK with this Tutorial
Hello World has been used to introduce many new programming languages, in this case it is an introduction to the Windows App SDK and will display a message when you Click on a Button.
Step 1
Follow Setup and Start on how to get Setup and Install what you need for Visual Studio 2022 and Windows App SDK.
Step 2
Step 3
In the Code for MainWindow.xaml.cs there will already be a Method of myButton_Click(...)
and within this the following Line should be Removed:
myButton.Content = "Clicked";
Step 4
Then in myButton_Click(...)
where myButton.Content = "Clicked";
was Removed type in the following:
await new ContentDialog()
{
XamlRoot = Content.XamlRoot,
Content = "Hello World",
PrimaryButtonText = "Close"
}
.ShowAsync();
This will create a ContentDialog
with the Content
of Hello World with the PrimaryButtonText
of Close
and uses the Method for ShowAsync
to display the ContentDialog
.
It also sets the XamlRoot
to allow the ContentDialog
to work correctly.
The Method of ShowAsync
uses the Keyword for await
which means it will perform a Task that won't happen at the same time, or asynchronously.
Step 5
While still in the Method for myButton_Click(...)
between private
and void
type in the following:
async
Because the Method for ShowAsync
is asynchronous using the Keyword of await so you need to mark the Method it is used in as such, this done with the Keyword of async
.
The Method for myButton_Click(...)
should look as follows:
private async void myButton_Click(object sender, RoutedEventArgs e)
{
await new ContentDialog()
{
XamlRoot = Content.XamlRoot,
Content = "Hello World",
PrimaryButtonText = "Close"
}
.ShowAsync();
}
When the Button is Clicked, the Method of myButton_Click(...)
will be triggered and this display a ContentDialog
with the Content
of Hello World.
Step 6
Step 7
Once running you should see the Button
with the text of Click Me
Step 8
If you Click on the Button
with the text, Click Me, it will display the ContentDialog
which you can then dismiss with the Button of Close.