UWP Development, Extension of Style

Introduction
UWP (Universal Windows Platform) development is a development platform that allows the same app to run on various Windows devices. One of the powerful features of UWP is the use of Styles. Styles define the visual properties of user interface (UI) elements, enhancing the consistency of applications and making maintenance easier. In this post, we will explain in detail how to extend styles in UWP and provide practical examples to aid understanding.

1. Basics of UWP Styles

Styles are declared using a ResourceDictionary defined in XAML. These styles define various properties of UI elements, making them reusable and helping to maintain the consistency of the UI. By default, styles consist of the following components:

  • TargetType: The type of UI element to which the style will be applied (e.g., Button, TextBox, etc.)
  • Setters: A set of Setter that set the values of properties defined in the style

1.1 Basic Style Example

<Page.Resources>
    <Style TargetType="Button">
        <Setter Property="Background" Value="SkyBlue"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Padding" Value="10,5"/>
    </Style>
</Page.Resources>

This example defines a basic style for a Button on a UWP page. By setting the background color, foreground color, font size, and padding of the button, it ensures that all buttons appear in a consistent manner.

2. Extending Styles

In addition to basic definitions, styles can be extended to suit specific situations. This allows the same UI element to have different visual representations depending on the context. There are various methods to extend styles, and here we will explain using Derived Styles.

2.1 Inheriting Styles

Style inheritance allows one style to be set as the base style, and other styles can be extended based on this. This can be implemented using the `BasedOn` property.

<Page.Resources>
    <Style x:Key="BaseButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="SkyBlue"/>
        <Setter Property="Foreground" Value="White"/>
    </Style>

    <Style TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}">
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Padding" Value="15,10"/>
    </Style>
</Page.Resources>

The second style created here can still use the properties of `BaseButtonStyle` while adding additional settings. This reduces code redundancy and simplifies maintenance.

2.2 State-Based Styles

State-based styles are a powerful tool for changing the appearance of UI elements based on user interactions. For example, they can provide visual feedback when a button is hovered over or clicked.

<Style TargetType="Button">
        <Setter Property="Background" Value="SkyBlue"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontSize" Value="16"/>

        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="DodgerBlue"/>
            </Trigger>

            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" Value="DarkBlue"/>
            </Trigger>
        </Style.Triggers>
    </Style>

This example defines a style that changes the background color of a button when hovered over or clicked. This provides immediate feedback to the user.

3. Customized Control Styles

We often want to change the styles of built-in controls to create a unique UI. In such cases, ControlTemplate can be used to redefine the entire structure of a control.

3.1 Defining a ControlTemplate

Here is an example of customizing a Button using its ControlTemplate:

<Button Content="Click Me">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>

In the above example, we replaced the default structure of the Button with a `Grid` and used a ContentPresenter to center the button’s content. The use of TemplateBinding allows us to change the structure completely while retaining the button’s background color.

3.2 Applying Additional Styles

Once a ControlTemplate is defined, it can be used alongside styles. You can still set properties through the style:

<Page.Resources>
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="SkyBlue"/>
            <Setter Property="Foreground" Value="White"/>
        </Style>
    </Page.Resources>

This style ensures that all buttons receive the specified background and foreground colors along with the customized ControlTemplate. This allows for easy user customization while maintaining UI consistency.

4. Advanced Styling Techniques

In UWP, various techniques allow for the creation of more sophisticated and polished UIs using styles. Here, we will cover data-based styles and triggered styles.

4.1 Data-Based Styles

DataBinding allows the style of UI elements to be represented differently based on data. This provides customized responses to users and flexibility for developers in maintenance.

<Page.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="{Binding ButtonColor}"/>
            <Setter Property="Foreground" Value="White"/>
        </Style>
    </Page.Resources>

Here, we use data binding to ensure that the button’s background color is determined by a property called ButtonColor. This approach is useful for creating a more dynamic UI.

4.2 Using Provided Triggers

Another advanced technique involves state transitions through the VisualStateManager. The VisualStateManager is a useful tool for switching visual representations based on a control’s state.

<VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal"/>
            <VisualState x:Name="PointerOver">
                <Storyboard>
                    <ColorAnimation Storyboard.TargetName="MyButton" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Yellow" Duration="0:0:0.2"/>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

The above code shows an example of changing the background color of a button based on its state. When entering the PointerOver state, the background color changes to yellow. This provides an intuitive interface for users.

5. Conclusion

The extension of styles in UWP is a powerful feature that helps developers create dynamic and responsive UIs for their applications. By combining various styling techniques, it is possible to provide a better user experience. This post covered the basic principles of styles, state-based styles, ControlTemplate, and data binding. We hope you leverage these techniques to develop unique and user-friendly apps.

Finally, learning all possible styles and properties in UWP styling can take time, but these techniques will significantly streamline your development process.

We hope that continuous learning about UWP development will be very beneficial to you. If you have any additional questions, feel free to reach out through ChatGPT!

UWP Development, Template

Universal Windows Platform (UWP) is a platform for developing applications on the Windows 10 operating system, allowing the creation of applications that can run on various devices. UWP apps can operate on desktops, tablets, Xbox, IoT devices, and more. In this post, we will explore various aspects of UWP development, particularly focusing on ‘Templates’. Utilizing templates in UWP apps is a crucial factor that enhances development speed, maintains consistent UI/UX, and increases code reusability.

1. Importance of UWP Templates

Templates can be useful in many ways when developing UWP applications. Templates provide a basic structure and design, making it easier for developers to get started. This can yield the following benefits:

  • Time Saving: Using predefined structures and styles can significantly enhance the speed of app development.
  • UI/UX Consistency: It helps maintain a cohesive user experience.
  • Code Reusability: Common components can be created for global use and reused whenever needed.

2. Types of UWP Templates

There are several types of templates available in UWP. Here, we will introduce some of the most commonly used template types.

2.1 Basic Template

The basic template provides the fundamental structure for a UWP app. It is the default app template that can be selected when creating a new project in Visual Studio. This template includes main files such as App.xaml and MainPage.xaml.

2.2 Blank Template

The blank template allows you to start from a clean slate for customization. With this template, developers can add the necessary elements to create a UI tailored to their needs.

2.3 Library Template

The library template is designed for code sharing and reuse. Using this template, you can write classes that contain common functionalities that can be used in other UWP projects.

3. Example of Implementing UWP Templates

Now that we understand the concept of UWP templates, let’s look at how we can use them in practice through code examples.

3.1 Basic App Template Example

Create a new UWP project in Visual Studio and select the ‘Blank App (Universal Windows)’ template. Below is the structure of the main files.

    Project
    ├── App.xaml
    ├── App.xaml.cs
    ├── MainPage.xaml
    └── MainPage.xaml.cs
    

Open the ‘MainPage.xaml’ file and add the code below to set up a simple UI.


    <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 Text="Hello, UWP!" 
                       FontSize="36" 
                       VerticalAlignment="Center" 
                       HorizontalAlignment="Center"/>
        </Grid>
    </Page>
    

3.2 Blank Template Example

When selecting the blank template, there is no basic code, so you need to write it yourself as shown below. First, create a new project and select ‘Blank App (Universal Windows)’. Then, write the following code in the MainPage.xaml file.


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

        <Grid Background="White">
            <Button Content="Click Me" 
                    Click="Button_Click" 
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center"/>
        </Grid>
    </Page>
    

Then, add the button click event handler in the ‘MainPage.xaml.cs’ file.


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

3.3 Library Template Example

To create a library instance, you create a ‘Class Library (Universal Windows)’ project in Visual Studio. This allows you to create reusable code and resources across multiple UWP applications.


// SampleClass.cs
namespace MyUWPAppLibrary
{
    public class SampleClass
    {
        public string GetMessage()
        {
            return "Hello from MyUWPAppLibrary!";
        }
    }
}
    

The library you create can then be referenced in other UWP application projects.

4. Ways to Utilize Templates

When using templates, there are various ways to leverage them.

  • Style Templates: Easily apply styles to UI elements like buttons and text boxes.
  • Data Templates: Templates used to display data in controls like ListView and GridView.
  • Control Templates: Define the visual structure of a UIControl for refactoring.

5. Conclusion

Now we have understood the role and importance of templates in UWP development and explored ways to implement them. By utilizing templates, you can reduce development time and effort while providing a more consistent user experience. When developing UWP apps in the future, actively use templates for more efficient development.

If you have any further questions or topics you would like to learn more about, please leave a comment!

UWP Development, Order of Style Application and Scope of Application

The Universal Windows Platform (UWP) is a framework for developing applications that run on the Windows 10 operating system. With UWP, developers can provide a consistent user experience across various devices. In this article, we will take a closer look at an important topic in UWP development: ‘The Order and Scope of Style Application.’

1. Overview of UWP Styling

In UWP, styles determine the visual representation of UI elements. Using styles helps to adjust the appearance of controls and create a consistent design. Styles are primarily defined in XAML and can be applied in various ways.

2. Order of Style Application

In UWP, styles are applied in a defined order. This order is very important. When applying styles, the following order should be considered:

  1. Control Defaults: All controls in UWP have a default style. This default style is applied differently based on the built-in themes in UWP.
  2. Local Styles: Styles defined locally take precedence over default styles. These styles can be applied only to specific pages or controls.
  3. Styles Defined in Application Resources: Styles used globally across the application are defined in the resource dictionary (e.g., App.xaml). These styles provide consistency across the application.
  4. Theme-based Styles: Windows provides several color themes, including dark mode and light mode. These theme styles are applied based on the currently selected theme.

3. Scope of Style Application

In UWP, styles can be applied in various scopes.

  • Global App: Styles that can be used throughout the application can be defined in the ResourceDictionary of the App.xaml file.
  • Page-level: Styles for use within individual pages can be defined. This applies only to the UI elements of a specific page.
  • Control-level: Styles can be applied only to specific UI controls. In this case, the style is defined directly within the XAML of that control.

4. Example: Defining and Applying Styles

Below is an example of defining a simple style that can be applied to an Edit Box and a Button.

<Application.Resources>
    <Style x:Key="MyButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="DodgerBlue"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Padding" Value="10,5"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="CornerRadius" Value="5"/>
    </Style>

    <Style x:Key="MyTextBoxStyle" TargetType="TextBox">
        <Setter Property="Background" Value="WhiteSmoke"/>
        <Setter Property="BorderBrush" Value="LightGray"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Padding" Value="8"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>
</Application.Resources>

The above code defines two styles. MyButtonStyle is a style for a button, and MyTextBoxStyle is a style for a text box. These styles are defined in Application.Resources for global use.

5. Example of Style Application

Here is how to apply the defined styles to UI elements:

<Grid>
    <Button Style="{StaticResource MyButtonStyle}" Content="Click Me"/>
    <TextBox Style="{StaticResource MyTextBoxStyle}" PlaceholderText="Enter text"/>
</Grid>

This code creates a Button and a TextBox with applied styles within a grid layout. The button uses MyButtonStyle, while the text box uses MyTextBoxStyle.

6. Conclusion

In UWP development, styles play an important role in creating a consistent user interface and enhancing branding. By understanding the order and scope of style application, developers can create more flexible and maintainable applications. If you gain real development experience through the style definitions and application methods discussed above, your UWP applications will be significantly improved.

We will continue to cover UWP development topics, sharing various styling techniques and best practices. Thank you!

UWP Development, Style

This article covers how to set styles in Universal Windows Platform (UWP) development on Windows, introducing the importance and application of styling through practical example code.

1. Introduction

UWP is a platform that allows the development of modern apps that run on various Windows 10 devices. UWP apps can apply various styles to provide users with a rich experience. This article will explain the basic concepts of styles, usage of styles in XAML, resource management, and advanced techniques related to styles.

2. Basic Concepts of Styles

Styles are a powerful feature that helps to apply a consistent visual to UI elements in UWP apps. By using styles, you can easily set common properties across multiple UI elements, which increases code reusability and reduces maintenance effort.

Styles are defined within XAML files using the Style element. An example of a basic style is as follows:


            <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
                <Style TargetType="Button">
                    <Setter Property="Background" Value="LightBlue" />
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="FontSize" Value="16" />
                    <Setter Property="Padding" Value="10" />
                </Style>
            </ResourceDictionary>
            

3. Applying Styles

Applying the defined style to UI elements is very simple. To apply a style, specify the desired style in the Style property of the respective UI element. The example below shows how to apply a style to a button:


            <Button Content="Click Here" Style="{StaticResource MyButtonStyle}"/>
            

Here, MyButtonStyle is the key of the style defined above. The StaticResource markup extension is used to statically reference the resource.

4. Typography and Color

In UWP apps, typography and color are also important elements of style settings. By adjusting various font styles and background colors, you can enhance the user experience.

For example, an example of applying typography style is as follows:


            <Style TargetType="TextBlock">
                <Setter Property="FontFamily" Value="Segoe UI" />
                <Setter Property="FontSize" Value="20" />
                <Setter Property="Foreground" Value="Black" />
            </Style>
            

5. Advanced Styling Techniques

Using advanced techniques in UWP styling is suitable for building complex UIs. Here, we will describe advanced techniques including triggers, data binding, and animations.

5.1 Using Triggers

Triggers allow styles to change based on specific conditions. For example, you can change the button’s background color when the mouse hovers over the button:


            <Style TargetType="Button">
                <Setter Property="Background" Value="LightBlue" />
                <Setter Property="Foreground" Value="White" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="DarkBlue" />
                    </Trigger>
                </Style.Triggers>
            </Style>
            

5.2 Data Binding

Data binding can be used in styles, allowing for dynamic updates of UI elements. For example, an example of binding color is as follows:


            <Style TargetType="Button">
                <Setter Property="Background" Value="{Binding ButtonBackgroundColor}" />
            </Style>
            

5.3 Applying Animations

Applying animations to UI elements can provide a more immersive user experience. The following is an example that gives an animation effect when a button is clicked:


            <Style TargetType="Button">
                <Setter Property="Opacity" Value="1" />
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0.5" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
            

6. Using Theme Resource Dictionaries

In UWP apps, resource dictionaries can be used to manage themes. This allows for dynamic changes between various themes. For example, it is possible to create an app that supports dark mode and light mode.


            <ResourceDictionary>
                <Color x:Key="PrimaryColor">#FF0078D7</Color>
                <Color x:Key="SecondaryColor">#FFD83B00</Color>
            </ResourceDictionary>
            

With the resources defined this way, the app can be easily adjusted to fit the theme.

7. Conclusion

Effectively using styles in UWP apps enables a consistent user experience while facilitating maintenance. In this article, we learned various styling methods through the basic concepts and application methods of styles, as well as advanced styling techniques. Since styling is an important element in UWP app development, it is essential to properly manage and utilize the styles of each element.

I hope this article has been helpful for UWP style development. If you have any additional questions or requests, please feel free to leave a comment!

UWP Development, Storyboard

UWP (Universal Windows Platform) development focuses on providing a consistent user experience across various Windows devices. Among these, the Storyboard is a useful tool for adding animations and changes to user interface (UI) elements, creating a more engaging and interactive environment. In this article, we will explain how to maximize UI effects using Storyboard within UWP applications and provide practical example code.

What is a Storyboard?

A Storyboard is a feature of UWP that supports structuring animations over time. This allows developers to change various UI properties, such as position, size, opacity, and color, over time. By utilizing Storyboards, users can experience a more vibrant UI, and the state changes of UI elements can be made smoother.

Components of a Storyboard

A Storyboard consists of the following components:

  • Animation: Each animation changes one UI property and can come in different types (e.g., DoubleAnimation, ColorAnimation, etc.).
  • Timeline: Defines the duration of the animation. You can set when the animation starts and ends.
  • Target: Specifies which UI element the animation will be applied to.
  • KeyFrames: Defines intermediate states of the animation over time. This allows for more complex animations.

Using Storyboard in UWP Applications

Here is how to use Storyboard in UWP applications.

1. Defining Storyboard in XAML

Storyboards are primarily defined in XAML. Below is a simple example of a Storyboard:

<Page ...>
    <Page.Resources>
        <Storyboard x:Name="MyStoryboard">
            <DoubleAnimation
                Storyboard.TargetName="MyRectangle"
                Storyboard.TargetProperty="Width"
                From="100" To="300" Duration="0:0:2" />
            <DoubleAnimation
                Storyboard.TargetName="MyRectangle"
                Storyboard.TargetProperty="Height"
                From="100" To="300" Duration="0:0:2" />
        </Storyboard>
    </Page.Resources>

    <Grid>
        <Rectangle x:Name="MyRectangle" Fill="Blue" Width="100" Height="100" />
        <Button Content="Start Animation" Click="OnStartAnimationClick" />
    </Grid>
</Page>

2. Starting Storyboard from C# Code

You can invoke the Storyboard from C# code to execute it. Below is an example of a button click event that starts the Storyboard:

private void OnStartAnimationClick(object sender, RoutedEventArgs e)
{
    MyStoryboard.Begin();
}

3. Using Complex Animations

Storyboard supports not only simple animations but also complex ones. For example, you can apply animations to multiple elements simultaneously or combine different types of animations.

<Storyboard x:Name="MyComplexStoryboard">
    <DoubleAnimation
        Storyboard.TargetName="MyRectangle"
        Storyboard.TargetProperty="Width"
        From="100" To="300" Duration="0:0:2" />
    <ColorAnimation
        Storyboard.TargetName="MyRectangle"
        Storyboard.TargetProperty="Fill.Color"
        From="Blue" To="Red" Duration="0:0:2" />
</Storyboard>

4. More Control with KeyFrames

Using KeyFrames allows for more precise control over the progress of the animation. Below is an example of using KeyFrames:

<Storyboard x:Name="MyKeyFrameStoryboard">
    <DoubleAnimationUsingKeyFrames Storyboard.TargetName="MyRectangle" Storyboard.TargetProperty="Width">
        <LinearDoubleKeyFrame Value="150" KeyTime="0:0:1" />
        <LinearDoubleKeyFrame Value="250" KeyTime="0:0:2" />
   </DoubleAnimationUsingKeyFrames>
</Storyboard>

Implementing Optimized UI Animations

While animations are a powerful tool to enhance user experience, excessive animations can actually hinder users. Therefore, when using animations, one should consider the following optimized approaches.

1. Understanding the Purpose of Animations

One should consider what information the animation can convey to the user. Well-designed animations help understanding, draw the user’s attention, and enhance the overall experience.

2. Maintaining Consistency

Animations should be consistent with the overall style of the application. This provides a smoother and more coherent user experience.

3. Performance Optimization

Since animations consume CPU and GPU resources, in applications where performance is critical, only the minimum necessary animations should be applied. For instance, performance can be optimized by removing unnecessary animations and using simpler animations.

Conclusion

In UWP development, Storyboards provide an attractive experience to users by adding animations and changes to UI elements. This article detailed the basic concepts of Storyboards and provided practical example code on how to utilize Storyboards. Now, you can develop useful animations to enhance user experience.

References