UWP Development, Custom Resource

Windows Universal Platform (UWP) development focuses on creating apps that run on various Windows 10 devices. One important aspect of UWP app development is creating reusable resources. Custom Resources help maintain consistency in the app, enhance code reusability, and facilitate easier maintenance later. In this article, we will explore how to create and utilize custom resources in UWP.

1. What is a Resource?

In UWP and XAML-based applications, a ‘resource’ is a collection of data used to define and style various components of the application. Resources can include colors, brushes, styles, templates, etc., and by defining these resources, developers can increase code efficiency and reuse common elements.

2. The Importance of Custom Resources

Custom resources are important for the following reasons:

  • Consistency: Styles can be easily applied to ensure that the app’s UI elements look consistent.
  • Reusability: By reusing the same resource in multiple places, code duplication can be reduced.
  • Maintenance: If a resource changes, the changes will automatically be reflected in all elements that use that resource.

3. Creating Custom Resources

3.1. Creating a Resource Dictionary

First, you need to create a Resource Dictionary where you will define your custom resources. To do this, create a XAML file and add code to define the resources.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Color x:Key="PrimaryColor">#FF6200EE</Color>
    <SolidColorBrush x:Key="PrimaryBrush" Color="{StaticResource PrimaryColor}" />

    <Style TargetType="Button" x:Key="PrimaryButtonStyle">
        <Setter Property="Background" Value="{StaticResource PrimaryBrush}" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Padding" Value="10" />
    </Style>

</ResourceDictionary>

In the example above, we defined a Resource Dictionary that includes a primary color and button style. This Dictionary can be reused across various parts of the app.

3.2. Using the Resource Dictionary

Once you’ve defined your resources, you can use them within your UWP app. First, here is how to reference the resource Dictionary in the App.xaml file:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/YourResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Here, ‘YourResourceDictionary.xaml’ is the filename of the resource dictionary you created. This will allow resources to be accessed globally throughout the app.

3.3. Example of Using a Resource

Here’s how to use the defined resource in a real UWP app:

<Button Content="Click Me!" Style="{StaticResource PrimaryButtonStyle}" />

This code applies the custom ‘PrimaryButtonStyle’ to the button, ensuring consistent styling.

4. Dynamic Resources

UWP also supports a feature called ‘Dynamic Resources’ that allows resources to be changed dynamically. Using dynamic resources, you can change resources while the application is running, and the changes are immediately reflected in the UI. Dynamic resources can be declared as follows:

<Button Content="Change Color" Background="{DynamicResource PrimaryBrush}" Click="OnChangeColorButtonClick" />

Let’s look at an example where clicking the above button changes the background color of the button:

private void OnChangeColorButtonClick(object sender, RoutedEventArgs e)
{
    // Change the existing resource to a new color
    Application.Current.Resources["PrimaryBrush"] = new SolidColorBrush(Colors.Red);
}

5. Conclusion

Utilizing custom resources can enhance productivity in UWP app development and maintain consistency in the UI. With what you’ve learned today, you should be able to easily implement and utilize custom resources in your apps. By defining and experimenting with various resources, you can add unique styles and flexibility to your apps.

To gain a deeper understanding of UWP development, it is recommended to refer to the official Microsoft documentation and related educational materials. In future courses, we will cover more possibilities of UWP, so we appreciate your interest!

UWP Development, Installing Visual Studio Community

Hello! In this post, we will take a detailed look at how to install Visual Studio Community for Windows Universal Platform (UWP) app development. UWP is a platform designed to enable the development of applications that can be universally used on Windows 10 and later versions. By following this guide, you will learn how to install the necessary tools and create your first UWP app.

Definition of UWP Development

UWP is a platform provided by Microsoft that allows developers to create applications for desktop, tablets, and mobile devices using a single code base. This enables a consistent user experience across a variety of devices.

UWP apps are distributed through the Windows Store and operate on Windows 10 devices, with built-in security and performance optimizations. The apps use XAML for UI development and implement business logic using C# or C++.

Installing Visual Studio Community

1. Download Visual Studio

Visual Studio Community is a free IDE that supports UWP development. To start the installation, follow these steps:

  1. Visit the Visual Studio Community download link.
  2. Click the ‘Download’ button on the page to download the installer.

2. Run the Installer

Run the downloaded installer. The setup wizard will start and various installation options will appear.

3. Select Workload

For UWP app development, you need to select the “Developer Workload”. Make sure to check the following:

  • Select the “Universal Windows Platform development” checkbox.
  • You can add other required components as needed, such as .NET desktop development or Azure development components.

After making your selections, click the “Install” button. Wait for the installation process to complete.

4. Verify After Installation Completion

Once the installation is complete, launch Visual Studio to ensure that the UWP app templates are displayed correctly. Click on ‘File’ > ‘New’ > ‘Project’ and find ‘Universal Windows Platform’ in the template list.

Creating Your First UWP App

1. Create a New Project

In Visual Studio:

  1. Select ‘File’ → ‘New’ → ‘Project’.
  2. Type ‘UWP’ in the search box and select ‘Universal Windows Platform App’.

2. Configure Project

After selecting your project name and location, click the ‘Create’ button and configure the following:

  • ‘Minimum OS Version’: This setting defines the minimum Windows version on which the app can run.
  • ‘Target OS Version’: Choose the Windows version that the app targets.
  • ‘Deploy to the Windows Store’: Select this option if the app will be distributed through the Windows Store.

3. Design the UI

The core UI of a UWP app is created using XAML. Open the MainPage.xaml file in Solution Explorer and write the code that shapes your UI. Below is an example code including a simple button:

<Page
    x:Class="MyFirstUWPApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyFirstUWPApp"
    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 Content="Click Me!" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click"/>
    </Grid>
</Page>

4. Handle Events

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

private void Button_Click(object sender, RoutedEventArgs e)
{
    Button button = sender as Button;
    button.Content = "Clicked!";
}

5. Run the App

To run the app, ensure there are no errors in the toolbar, then press F5 to start in debugging mode. A new UWP app created by Windows will run, and you can click the button.

Advantages of UWP Development

  • Single Codebase: You can deploy to various devices with a single code.
  • Modern UI Components: Supports various UI components and modern designs.
  • Powerful APIs: Gives access to a variety of Windows APIs.
  • Windows Store Deployment: Apps can be easily deployed and managed.

Conclusion

We have explored how to install Visual Studio Community for UWP app development and how to create your first app. UWP is a powerful platform from Microsoft that provides opportunities to develop innovative apps for various devices. Through further learning, you can deepen your understanding of UWP’s features and leverage them.

UWP Development, Understanding Code Behind Merged with View and ViewModel

Understanding Code Behind Merged Views and ViewModels in UWP Development

UWP (Universal Windows Platform) development is a powerful platform for building applications that can run on a variety of Microsoft devices. The MVVM (Model-View-ViewModel) architecture is widely used in UWP application development, and understanding the interaction between UWP’s view and view model is crucial.

1. Understanding the MVVM Architecture

MVVM is a design pattern implemented in UWP applications that clearly defines the structure of the application and separates the code to facilitate maintenance and testing. MVVM is primarily composed of the following three components:

  • Model: Contains the application’s data and business logic.
  • View: Represents the user interface (UI) and interacts with the user.
  • ViewModel: Acts as an intermediary between the view and model, preparing the data needed for the view and handling user actions.

This design pattern increases code reuse and enables more efficient application development through the separation of UI and business logic.

2. Combining View and ViewModel in UWP

In UWP, views are defined using XAML, while C# is used on the backend to implement the view models. The view and view model are connected via data binding. Through data binding, the view can observe the properties of the view model in real-time, and events that occur in the view (e.g., button clicks) are passed to the view model to perform appropriate logic.

2.1. Features of Data Binding

The data binding in UWP has the following characteristics:

  1. Data Context: Assign a view model instance using the DataContext property of the view.
  2. Property Change Notification: By implementing the INotifyPropertyChanged interface, notifications can be sent to the UI when properties change.
  3. Commands: Use the ICommand interface to pass events occurring in the view as commands to the view model.

2.2. Example Code

Let’s examine the combination of views and view models in a UWP application through the following example.

 
// Model
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

// ViewModel
using System.ComponentModel;

public class PersonViewModel : INotifyPropertyChanged
{
    private Person person;
    
    public PersonViewModel()
    {
        Person = new Person { Name = "John Doe", Age = 30 };
    }

    public Person Person
    {
        get => person;
        set
        {
            person = value;
            OnPropertyChanged(nameof(Person));
        }
    }

    public string DisplayName => $"{Person.Name}, {Person.Age} years old";

    public event PropertyChangedEventHandler PropertyChanged;

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



    
        
        
    

3. Data Transfer from ViewModel to View

Now let’s look at how to bind data from the view model to update it in the view. We will add a simple logic to change the property of the view model on button click.

 
// MainPage.xaml.cs
private PersonViewModel viewModel;

public MainPage()
{
    this.InitializeComponent();
    viewModel = new PersonViewModel();
    this.DataContext = viewModel;
}

private void OnChangeNameClicked(object sender, RoutedEventArgs e)
{
    viewModel.Person.Name = "Jane Doe"; // Change name
    viewModel.OnPropertyChanged(nameof(viewModel.DisplayName)); // Update DisplayName
}

4. Conclusion

Now we have understood the integration and data flow between views and view models in UWP. The MVVM architecture increases the maintainability of the code and allows for a clear separation of UI and application logic. In this article, we explained the interaction between views and view models through a basic example. Building on this foundation, more complex applications can be developed.

More information about UWP development will be provided in the future, so please stay tuned!

UWP Development, Outputting Numbers on Repeated Buttons in the Body

In today’s software development environment, the intuitive experience for users is increasingly emphasized. In particular, UWP (Universal Windows Platform) focuses on providing a consistent user experience across various devices. In this course, we will cover how to implement a feature that ‘displays a number on repeated buttons’ while developing UWP applications.

Objectives

Through this course, users will implement the following features:

  • A function that increments a number every time the user clicks a button
  • How to dynamically create UI elements to manage multiple buttons and the displayed numbers
  • How to structure UWP applications using XAML and C#

Setting Up the Development Environment

Before starting UWP application development, the following development environment must be set up:

  • Windows 10 or later
  • Visual Studio 2019 or later
  • Installation of Visual Studio components that support UWP development

Creating a Project

Open Visual Studio and follow the steps to create a new UWP project:

  1. Select ‘New’ from the ‘File’ menu and click on ‘Project’.
  2. Choose ‘Blank App’ and then select the UWP app template.
  3. Set the project’s name and storage location, then click the ‘Create’ button.

Structuring the XAML Layout

Now, let’s modify the XAML file to set up a space to display buttons and numbers. Open the MainPage.xaml file and change it as follows:

<Page
    x:Class="YourAppNamespace.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:YourAppNamespace"
    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}">
        <StackPanel x:Name="ButtonContainer">
            <TextBlock x:Name="CounterText" FontSize="30" Text="Number: 0" />
        </StackPanel>
    </Grid>
</Page>

In the above code, we used StackPanel to stack the buttons and numbers. We also used the TextBlock element to display the current number.

Writing C# Code

After modifying the XAML file, we will write C# code to implement the button click events and number increment logic. Open the MainPage.xaml.cs file and add the code below:

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

namespace YourAppNamespace
{
    public sealed partial class MainPage : Page
    {
        private int buttonCount = 0;

        public MainPage()
        {
            this.InitializeComponent();
            SetupButtons();
        }

        private void SetupButtons()
        {
            for (int i = 0; i < 5; i++)
            {
                Button btn = new Button();
                btn.Content = $"Button {i + 1}";
                btn.Width = 200;
                btn.Click += Button_Click;
                ButtonContainer.Children.Add(btn);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            buttonCount++;
            CounterText.Text = $"Number: {buttonCount}";
        }
    }
}

In the code above, we:

  • Called the SetupButtons method in the constructor to dynamically create the buttons.
  • Connected a click event to each button so that when the user presses a button, the Button_Click method is triggered.
  • Incremented the buttonCount variable on button clicks and updated the TextBlock text to display the current value.

Checking the Results

Now, build and run the project to check the results. When the app runs, the created buttons will be displayed on the screen. You will see the number increasing every time you click a button. Through this process, we learned some important concepts of UWP:

  • Dynamic UI structure
  • Event handling
  • Data binding and updating

Additional Practice Problems

Now that the basic implementation is complete, consider the following additional practice problems to deepen your understanding:

  • Modify the functionality to allow the user to input the total number of buttons to create that many buttons.
  • Add a button to decrease the number and a reset button to create various states.
  • Research how to add animation effects on button clicks to provide visual feedback to users.

Conclusion

This course covered how to implement repeated buttons that display numbers using UWP development. We learned how to handle events and create dynamic UI through basic XAML layout structure and C# code writing. UWP is a great platform for creating applications that can be used across various devices, providing users with a better experience.

Now you have the foundational knowledge necessary to develop various apps using UWP. If you wish to implement more advanced features, consider learning about the diverse APIs and patterns of UWP.

In the next course, we will cover other advanced features and best practices of UWP, so please stay tuned.

Thank you.

UWP Development, Change the Output Order of Repeated Buttons in the Body

UWP (Universal Windows Platform) is a platform provided by Microsoft for building applications on Windows 10 and later versions. With UWP, you can deliver a more consistent user experience across a variety of devices. In this article, we will explore in detail how to dynamically change the output order of repeating buttons among the UI components in UWP development. This will increase the flexibility of the user interface and help you learn to configure apps tailored to user needs.

1. Creating a UWP Project

First, you need to create a basic UWP project using Visual Studio. You can follow these steps:

  1. Run Visual Studio and select <New Project>.
  2. Search for <UWP> and select <Blank App> to create the project.
  3. Complete the project setup and click <Create>.

2. Basic UI Configuration in XAML

To configure the basic UI, open the XAML file and add controls to place the buttons that will be output repetitively. Here, we will use a StackPanel to list the buttons.

<Page
    x:Class="ButtonOrderApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ButtonOrderApp"
    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}">
        <StackPanel x:Name="ButtonPanel" Orientation="Vertical">

        </StackPanel>
    </Grid>
    

3. Creating and Outputting Buttons

To dynamically create buttons and add them to the StackPanel, write a method in C# code to create the buttons. Add the following code to the MainPage.xaml.cs file.

using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using System.Collections.Generic;
    using System.Diagnostics;

    namespace ButtonOrderApp
    {
        public sealed partial class MainPage : Page
        {
            private List

4. Changing the Output Order of Buttons

To change the output order of the buttons, we will add logic to move a specific button to the last position whenever a user clicks it. To do this, we will modify the Button_Click method.

private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Move the clicked button to the end of the StackPanel
        Button clickedButton = sender as Button;

        // Remove the button from ButtonPanel
        ButtonPanel.Children.Remove(clickedButton);

        // Add the button to the end of ButtonPanel
        ButtonPanel.Children.Add(clickedButton);
        
        // Output for the button click
        Debug.WriteLine($"{clickedButton.Content} clicked");
    }
    

5. Testing the App

Now that we have written all the code, let’s run the app to check its functionality. Press the F5 key to run the app in debug mode, and try clicking the buttons. The clicked button will move to the end of the list, changing the output order.

6. Code Explanation

To briefly explain the code:

  • CreateButtons: Creates buttons from 1 to 5 and adds them to the StackPanel.
  • Button_Click: This method is called when a button is clicked, removing the clicked button from the StackPanel and adding it again to change the order.

7. Additional Improvements

Currently, the order changes with each button click, but various enhancements could be added for more UI design diversity. For example:

  • Add animations on button click
  • Change the color or size of buttons to capture user visual interest
  • Fix certain buttons to always remain in a designated position

8. Conclusion

In this post, we learned how to change the output order of repeating buttons using UWP. This was a good example demonstrating that dynamic UI behavior can be implemented with simple code. Such techniques can greatly help improve the user experience of an app. Practice the code and add your enhancements to develop an even better application.

9. References