Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

Universal Windows Platform (UWP) -simple example UWP app written in C#

Universal Windows Platform (UWP) -simple example UWP app written in C#

Universal Windows Platform (UWP) allows you to build apps that can run on various Windows devices. Here's a simple example of a UWP app written in C#. This sample app will display a button, and when clicked, it will show a message dialog.

### Step-by-Step Guide

1. **Install Visual Studio**: Ensure you have Visual Studio installed with the UWP development workload.

2. **Create a New UWP Project**:
   - Open Visual Studio.
   - Click on `Create a new project`.
   - Search for `Blank App (Universal Windows)`, select it, and click `Next`.
   - Name your project, choose a location, and click `Create`.
   - Select the target and minimum versions of Windows, then click `OK`.

3. **Add UI Elements**:
   - Open `MainPage.xaml`.
   - Replace the existing XAML code with the following:

```xml

<Page

    x:Class="MyUWPApp.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:MyUWPApp"

    xmlns: d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">


    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Button x:Name="myButton" Content="Click Me" HorizontalAlignment="Center" VerticalAlignment="Center" Click="MyButton_Click"/>

    </Grid>

</Page>

```


4. **Add Click Event Handler**:
   - Open `MainPage.xaml.cs`.
   - Replace the existing code with the following:

```csharp
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;

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

        private async void MyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("Hello, UWP!");
            await messageDialog.ShowAsync();
        }
    }
}
```

5. **Run the App**:
   - Press `F5` to build and run the app.
   - Click the "Click Me" button, and a message dialog with "Hello, UWP!" will appear.

### Explanation

- **MainPage.xaml**: This file defines the user interface. A `Button` is placed in the center of the page.
- **MainPage.xaml.cs**: This file contains the code-behind for the XAML file. It defines the event handler `MyButton_Click` which gets called when the button is clicked. The event handler shows a message dialog with the text "Hello, UWP!".

This simple UWP app demonstrates the basics of creating a UI, handling events, and displaying a message. You can expand on this by adding more UI elements, handling other events, and incorporating more complex logic.

caa May 19 2024 167 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?