UWP Development, Creating Models

In recent years, the Universal Windows Platform (UWP) has played a significant role in providing a consistent user experience across various devices. One of the key elements of UWP development is creating an application’s data model. The data model defines how data is structured and managed within the application, clarifying the distinction between data and UI by applying the MVVM (Model-View-ViewModel) pattern. In this article, we will explore in detail how to structure a data model in a UWP application.

1. Understanding UWP and the MVVM Pattern

UWP is a platform for developing apps that can run on various Windows devices. MS’s MVVM pattern is useful for simplifying management and increasing code reusability. The MVVM pattern consists of three elements:

  • Model: Defines the data structure of the application. Manages interactions with the database and constraints on the data.
  • View: Comprises the elements of the user interface (UI) that interact with the user. In UWP, XAML is used to define the UI.
  • ViewModel: Manages data binding between the Model and View. The ViewModel acts as a bridge between the UI and data, managing the state of the UI.

2. Designing the Data Model

When designing a data model, you need to consider the structure of the required data and how to implement it in code. For instance, let’s assume we are creating a simple Todo list application. Each Todo item should have a title and a completion status.

2.1. Defining the Todo Model Class

using System;
using System.ComponentModel;

namespace TodoApp.Models
{
    public class TodoItem : INotifyPropertyChanged
    {
        private string title;
        private bool isCompleted;

        public string Title
        {
            get { return title; }
            set
            {
                if (title != value)
                {
                    title = value;
                    OnPropertyChanged("Title");
                }
            }
        }

        public bool IsCompleted
        {
            get { return isCompleted; }
            set
            {
                if (isCompleted != value)
                {
                    isCompleted = value;
                    OnPropertyChanged("IsCompleted");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

In the above code, we defined the TodoItem class. This class implements the INotifyPropertyChanged interface to notify the UI when a property changes. This enables data binding between the ViewModel and UI in the MVVM pattern.

3. Implementing the TodoListViewModel Class

Now it’s time to implement the ViewModel to manage the TodoItem model.

3.1. Defining the TodoListViewModel Class

using System.Collections.ObjectModel;

namespace TodoApp.ViewModels
{
    public class TodoListViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<TodoItem> todoItems;
        private TodoItem selectedTodo;

        public ObservableCollection<TodoItem> TodoItems
        {
            get { return todoItems; }
            set
            {
                if (todoItems != value)
                {
                    todoItems = value;
                    OnPropertyChanged("TodoItems");
                }
            }
        }

        public TodoItem SelectedTodo
        {
            get { return selectedTodo; }
            set
            {
                if (selectedTodo != value)
                {
                    selectedTodo = value;
                    OnPropertyChanged("SelectedTodo");
                }
            }
        }

        public TodoListViewModel()
        {
            TodoItems = new ObservableCollection<TodoItem>();
        }

        public void AddTodo(string title)
        {
            TodoItems.Add(new TodoItem { Title = title, IsCompleted = false });
        }

        public void RemoveTodo(TodoItem todo)
        {
            if (TodoItems.Contains(todo))
            {
                TodoItems.Remove(todo);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

In the code above, TodoListViewModel uses ObservableCollection to manage the list of Todo items. It also defines methods for adding and removing Todo items.

4. Building the UI with XAML

Now, let’s implement the user interface based on the model and ViewModel we created. In UWP, you can use XAML to build the UI.

4.1. Setting up MainPage.xaml

<Page
    x:Class="TodoApp.Views.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TodoApp.ViewModels"
    DataContext="{Binding TodoListViewModel, Source={StaticResource Locator}}">

    <StackPanel Margin="20">
        <TextBox x:Name="TodoInput" Placeholder="Enter a new todo item" />
        <Button Content="Add Todo" Click="AddTodo_Click" />
        <ListView ItemsSource="{Binding TodoItems}" SelectedItem="{Binding SelectedTodo}" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding IsCompleted}" />
                        <TextBlock Text="{Binding Title}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>

The above XAML code allows users to add Todo items through a text box and button while displaying the list in a ListView. Each item has a checkbox to indicate its completion status.

5. Handling Events and Implementing Functionality

Now we will connect the UI and ViewModel, handling the button click event so that users can add Todo items.

5.1. Implementing the Event Handler in MainPage.xaml.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

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

        private void AddTodo_Click(object sender, RoutedEventArgs e)
        {
            var viewModel = (TodoListViewModel)this.DataContext;
            string title = TodoInput.Text;

            if (!string.IsNullOrWhiteSpace(title))
            {
                viewModel.AddTodo(title);
                TodoInput.Text = string.Empty; // Clear the input field
            }
        }
    }
}

In the C# code above, we implemented the logic to add a Todo item when the button is clicked. After entering the title in the input field and clicking the button, the new Todo item is added to the ListView.

6. Summary and Optimization

The process of creating models in UWP applications is essential for structuring and managing data. The MVVM pattern allows for efficient interaction while maintaining a separation between data and UI. In this example, we implemented a simple Todo list application, which provided a deeper understanding of the structure and operation of UWP applications.

As we develop more complex applications in the future, it will be necessary to evolve and optimize the model and ViewModel. The most important thing is to create a user-friendly and intuitive UI, keeping in mind the user experience.

7. Additional Resources

If you want more in-depth information, please refer to the links below:

Through this article, I hope you have established a foundation for UWP development and have a clear understanding of how to design and implement a data model.

UWP Development, Markup Extension

Last updated: October 12, 2023

Introduction

Universal Windows Platform (UWP) is a heterogeneous platform developed by Microsoft that provides the tools and APIs necessary for creating applications that run on various Windows 10 devices. UWP applications use a specific UI markup language called XAML (Extensible Application Markup Language) to define their UI. This article presents an in-depth explanation of UWP’s markup extensions along with example code.

Basics of XAML

XAML is used to define all UI components such as UI elements, data binding, and styles. By using XAML, you can declaratively build a UI, and UWP inherently supports various types of UI elements and controls. The syntax of XAML is similar to HTML, but it is designed to operate within the Windows environment.

Example: Basic XAML 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 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="Hello, UWP!" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"/>
    </Grid>

</Page>

The Need for Markup Extensions

UWP provides a rich set of UI components by default. However, to meet specific requirements, it may be necessary to extend existing markup languages or create custom controls. Markup extensions can provide ongoing solutions for these needs and enhance UI reusability.

For example, you can create a custom button to implement specific styles or behaviors. This increases code reusability and improves maintainability.

Creating Markup Extensions

Now, let’s look at how to actually create markup extensions. Custom controls can be created by inheriting from UserControl and can be used alongside existing XAML.

Example: Creating a Custom Button

Below is the process for creating a custom button. This button can dynamically set text and background color.

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

    <Border Background="{Binding BackgroundColor, ElementName=customButton}" CornerRadius="5" Padding="10">
        <TextBlock Text="{Binding ButtonText, ElementName=customButton}" Foreground="White" FontSize="16"/>
    </Border>

</UserControl>

Code Behind: CustomButton.xaml.cs

using Windows.UI.Xaml.Controls;

namespace MyApp
{
    public sealed partial class CustomButton : UserControl
    {
        public static readonly DependencyProperty ButtonTextProperty =
            DependencyProperty.Register("ButtonText", typeof(string), typeof(CustomButton), null);
        
        public string ButtonText
        {
            get { return (string)GetValue(ButtonTextProperty); }
            set { SetValue(ButtonTextProperty, value); }
        }

        public static readonly DependencyProperty BackgroundColorProperty =
            DependencyProperty.Register("BackgroundColor", typeof(Windows.UI.Color), typeof(CustomButton), null);
        
        public Windows.UI.Color BackgroundColor
        {
            get { return (Windows.UI.Color)GetValue(BackgroundColorProperty); }
            set { SetValue(BackgroundColorProperty, value); }
        }

        public CustomButton()
        {
            this.InitializeComponent();
        }
    }
}

Usage

You can define how to use the custom button as follows.

<local:CustomButton ButtonText="Click Me" BackgroundColor="Blue" />

Considerations When Creating Markup Extensions

When creating markup extensions, there are several important considerations. First, performance. Custom controls can sometimes lead to performance degradation, so it’s advisable to use them only when necessary. Second, accessibility. Custom controls should be developed with accessibility in mind and should be compatible with screen readers.

Lastly, readability. As custom markup becomes more complex, it may become harder to use, so it is beneficial to document it well and provide sufficient examples.

Conclusion

Markup extensions in UWP enhance the flexibility and reusability of UI components, allowing users to meet more specific requirements. By creating custom controls, UWP applications can become more attractive, and these methods can increase efficiency in team collaborations.

The development of UWP will continue to grow, and it is important to enhance understanding and skills in this area. I hope this article helps you in your UWP development journey.

UWP Development, Distributing Without Uploading to Microsoft Store

UWP (Universal Windows Platform) is a powerful platform that allows for the easy development of applications that can run on various Windows 10 devices. While many developers publish their applications to the Microsoft Store for distribution, there are situations where applications need to be distributed through other means. This article will detail how to deploy UWP applications without publishing them to the Microsoft Store.

1. Setting Up the UWP Development Environment

Before starting UWP application development, you need to install Visual Studio and the Windows 10 SDK. The latest version of Visual Studio must be installed, and the UWP development workload should be selected for installation.

1. Download and install Visual Studio.
2. During installation, select "UWP Development" in the "Workloads" section.
3. The Windows 10 SDK will be installed automatically.

2. Application Development

The process of developing a UWP application is as follows.

2.1. Creating a New Project

1. Launch Visual Studio.
2. Click "Create a new project."
3. Search for "UWP" and select "Blank App."
4. Enter the project name and location, then click "Create."

2.2. Configuring the Basic UI

When you create a blank app, the basic App.xaml and MainPage.xaml files are generated. Let’s add the first UI component to the MainPage.xaml file.

<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">

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

In the code above, we add a button and set up an event handler to handle the click event.

2.3. Implementing the Event Handler

private void Button_Click(object sender, RoutedEventArgs e)
{
    var dialog = new MessageDialog("Button was clicked!");
    await dialog.ShowAsync();
}

2.4. Running and Testing the App

Press the F5 key to run the app and check if the dialog appears when the button is clicked.

3. Methods for Deploying UWP Applications

There are various ways to deploy UWP applications without publishing them to the Microsoft Store. The most common methods are to create an App Package for use or to use sideloading.

3.1. Creating an App Package

1. Click the "Build" menu in Visual Studio.
2. Select "Build Solution" to build the application.
3. Choose "File" menu -> "Publish" -> "Create App Package."
4. Follow the package creation wizard to enter the required information, and ultimately create the package.

The generated App Package will be provided in the form of .appx or .msix files. You can use this file to install on other devices.

3.2. Installation via Sideloading

Sideloading is a method of installing UWP apps without going through the Microsoft Store. To do this, developer mode must be enabled.

1. Open Windows Settings.
2. Go to "Update & Security" > "For developers."
3. Enable "Developer mode."

Now the sideloading setup is complete. To install the generated App Package, run the following command using PowerShell.

Add-AppxPackage -Path "C:\path\to\yourpackage.appx"

3.3. Deployment Using PowerShell

You can deploy the UWP app to selected devices using PowerShell. Run PowerShell as an administrator with the appropriate permissions and use the following command.

Invoke-Command -ComputerName "TargetPC" -ScriptBlock {
    Add-AppxPackage -Path "C:\path\to\yourpackage.appx"
}

4. Packaging and Signing

To safely distribute UWP apps, these App Packages require a digital signature. Additional security can be enhanced with signed apps. To sign, you need to create a certificate and use it to sign the package.

1. Use MakeCert.exe to create a certificate.
2. Use SignTool.exe to sign the package.

5. Conclusion

We have explored how to deploy UWP applications without publishing them to the Microsoft Store. You can deploy them by creating App Packages and using sideloading, and can implement it across multiple devices using PowerShell. Choose the appropriate deployment method that fits each business environment and need to utilize UWP applications more effectively.

6. Additional Resources

Such deployment methods can be utilized in various ways depending on the development environment or business needs. As part of an in-depth process of UWP development, we encourage you to apply it effectively to your projects.

UWP Development, Distributing by Uploading to the Microsoft Store

Universal Windows Platform (UWP) development provides a powerful way to create applications within Microsoft’s ecosystem. This article will explain the process of developing UWP apps and distributing them on the Microsoft Store in detail. This process includes the stages of app development, testing, packaging, and final distribution. This will provide a comprehensive understanding of UWP app deployment.

1. What is UWP?

UWP (Universal Windows Platform) is a framework that supports developers in creating applications that can run on various Microsoft devices. UWP-based apps run on Windows 10 and later versions, and can operate on a variety of platforms such as PCs, tablets, smartphones, Xbox, and HoloLens, using the same codebase. One of the biggest advantages of UWP is that it can provide a consistent user experience across multiple devices. Additionally, apps can be distributed on the Microsoft Store to reach users worldwide.

2. Setting Up the UWP Development Environment

To develop UWP apps, you first need to set up the development environment. Here are the basic installation steps.

  1. Install Visual Studio: Visual Studio is required for UWP app development. Download and install Visual Studio 2019 or later.
  2. Select Required Workloads: During the installation of Visual Studio, select the “UWP Development” workload. This will install the tools and packages necessary for developing UWP apps.
  3. Set Up a Testing Device: UWP apps can be tested on actual devices, which must be set to developer mode. While you can also test on a standard PC, it is advisable to test on other platforms for compatibility across different devices.

3. Basic Example of UWP App Development

3.1 Creating a Basic App

Here is how to create a new UWP app project using Visual Studio.

  1. Open Visual Studio and select “Create a New Project”.
  2. Type “Blank App (Universal Windows)” in the search box and select the project.
  3. Set the project name and location, then click the “Create” button.
  4. Set the target version and minimum version, then click “OK”.

Once the project is created, a MainPage.xaml file is generated by default. You can add UI elements here to build the app’s structure.

3.2 Adding UI Elements

Below is how to add basic UI elements like a button and text block.

<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}">
        <TextBlock x:Name="GreetingText" Text="Hello, UWP App!" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,50,0,0"/>
        <Button Content="Click Here" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Page>

The code above adds a text block and a button to the page. You will need to add the event handler method for the button click.

3.3 Creating an Event Handler

Add a method to handle the button click event in the MainPage.xaml.cs file.

private void Button_Click(object sender, RoutedEventArgs e)
{
    GreetingText.Text = "The button has been clicked!";
}

Now, when you run the app and click the button, the text block’s content will change. This is a simple example that helps in understanding the basic functionality of a UWP app.

4. Testing UWP Apps

After developing the app, it is essential to perform testing. You can run it in “Debug” mode in Visual Studio to test the app on the basic emulator or connected devices. Here is the testing process.

  1. Run the app. When you run it in debug mode, it will by default execute on the emulator or connected devices.
  2. Review each feature of the app, ensuring that all UI elements are displayed correctly.
  3. Click on the button and other user interaction elements to ensure that they function as expected.

5. Preparing for App Packaging and Distribution

If the app works correctly, you can now prepare for packaging and distribution. Packaging compresses the app into the .appx format for distribution. To do this, follow these steps.

5.1 Creating a Package

  1. From the Visual Studio menu, select “Project” – “Create and Distribute Project”.
  2. Select the packaging options, then click “Next”.
  3. Enter information such as app name, version, architecture, etc.
  4. Click “Package” to generate the .appx file.

5.2 Signing the Package

To distribute through the Microsoft Store, the package must be signed. This ensures the app’s reliability. To sign the package, you need to create a developer certificate and use it.

dotnet publish -f win10 -c Release --self-contained

6. Distributing to the Microsoft Store

Once packaging and signing are complete, you are ready to distribute the app to the Microsoft Store. The distribution process is as follows.

6.1 Registering a Developer Account

You need to register a developer account to distribute the app to the Microsoft Store. Create an account and log in, then navigate to the developer dashboard.

6.2 Submitting the App

  1. Select “New App” in the dashboard, then enter the app information.
  2. Upload the package and add marketing materials such as app description, screenshots, and icons.
  3. Set the app’s price, country, and system requirements, then click the “Submit” button.

6.3 Review and Approval

The submitted app undergoes Microsoft’s review process, during which the app’s functionality and user experience are evaluated. After the review is completed, you will be notified of the approval or rejection. The approved app will be distributed on the store.

7. Conclusion and Additional Resources

Developing and distributing an app using UWP may seem complex at first, but by systematically following each step, it can be successfully accomplished. UWP is a powerful platform that can provide a consistent user experience across multiple devices, which offers opportunities to connect with users worldwide.

If you wish to gain a deeper understanding of UWP development, please refer to the following resources:

I hope this article has been helpful in UWP development and distributing apps to the Microsoft Store. Wishing you success in your app development!

UWP Development, Creating a New App for the Microsoft Store

UWP (Universal Windows Platform) is an application framework developed by Microsoft that allows you to create apps using the same code across various Windows devices. UWP supports a wide range of platforms such as desktop, tablet, mobile, and Xbox, enabling developers to easily deploy apps that work in all environments. In this article, we will look at the basic concepts of UWP app development and detail the process of distributing apps through the Microsoft Store.

1. Overview of UWP

UWP applications run on Windows 10 and later versions and are distributed through the Windows Store. The main features of UWP include:

  • Execution on various devices: UWP runs on a wide range of Windows devices, including PCs, tablets, Xbox, and IoT devices.
  • Modern UI: It offers a sleek user interface (UI) based on the Fluent Design system.
  • Integration with smartphone apps: UWP is compatible with Windows 10 Mobile, allowing the expansion of smartphone apps.

2. Setting Up a UWP Development Environment

To begin UWP development, you need a Visual Studio environment. Below is how to install Visual Studio and set up a UWP development environment:

2.1 Installing Visual Studio

  1. Visit the Visual Studio download page.
    Visual Studio Download
  2. Run the Visual Studio installer, and in the Workload Selection step, check the Desktop development with C# checkbox.
  3. In the Developer Tools section, select Universal Windows Platform Development.
  4. After completing the installation, launch Visual Studio.

2.2 Creating a New UWP Project

  1. After running Visual Studio, select File > New > Project.
  2. In the search box, type Blank App (Universal Windows), select it, and click Next.
  3. Set the project name and location, then click the Create button.
  4. Select the target version and minimum version, then click OK.

3. Designing and Developing UWP Apps

Let’s look at the basic structure of a UWP app. UWP apps primarily use XAML (Extensible Application Markup Language) to design the UI and C# or C++ to implement app logic.

3.1 Designing UI with XAML

You can create the user interface of a UWP app using XAML. Below is an example of XAML code that includes a basic UI:

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

            <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
                <TextBlock Text="Hello, UWP!" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <Button Content="Click Me" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,20"/>
            </Grid>
        </Page>
        
        

3.2 Implementing App Logic with C#

Use C# code-behind to configure the logic for interacting with UI elements. Below is code that handles the button click event:

        
        using Windows.UI.Xaml;
        using Windows.UI.Xaml.Controls;

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

                private void Button_Click(object sender, RoutedEventArgs e)
                {
                    TextBlock textBlock = (TextBlock)FindName("MyTextBlock");
                    textBlock.Text = "Button Clicked!";
                }
            }
        }
        
        

4. Testing and Debugging the App

Testing and debugging are crucial processes during UWP app development. You can easily test it through Visual Studio.

4.1 Local Testing

  1. Click the Debug button in the upper menu to run the app.
  2. Alternatively, you can press the F5 key to run in debug mode.

4.2 Using the Emulator

You can test the operation on various devices using the Windows Holographic or Mobile emulator. To set up the emulator, you can add it through Visual Studio by going to Tools > Android Emulator Manager.

5. Distributing the App to the Microsoft Store

Once app development and testing are complete, the last step is to distribute the app to the Microsoft Store. Below are the steps in the distribution process:

5.1 App Packaging

  1. Configure a Release Build in Visual Studio.
  2. Select Build > [Project Name] > Create Package > Create App Package from the menu.
  3. In the App Package creation wizard, select the location to save and various options for generating the package.

5.2 Registering with Dev Center and Submitting the App

To submit your app to the Microsoft Store, you need to register for a developer account in the Azure Dev Center. After registration, you can submit the app through the following procedure:

  1. Log in to the Dev Center and go to My Apps.
  2. Click Create New App and enter the app name and information.
  3. Upload the app package and configure the business model and pricing.
  4. Click the Submit button to request a review from Microsoft.

6. Conclusion

With the UWP platform, you can easily develop apps that can be used across various Windows devices and distribute them to users worldwide through the Microsoft Store. I hope the content covered in this article has helped you understand the basic flow of UWP app development. The next steps could be to improve UI/UX, or explore how to expand app functionality by connecting to a database or API.