The Fluent Design System is a set of application design principles created by Microsoft and implemented across multiple desktop, mobile, and web platforms. The Fluent Design System for Windows is the set of controls, patterns, and styles for applications built for Windows 10 and Windows itself. In fact, it is the implicit styling for all WinUI controls.
It is important to learn the tenets of Fluent Design and how to implement these in your WinUI applications. We will also explore the Fluent XAML Theme Editor application for Windows. This application assists developers in creating a theme for their applications, including color schemes and style elements such as borders and corners. Developers can then easily import the resources to implement the theme.
In this chapter, we will cover the following topics:
By the end of this chapter, you will understand the Fluent Design System for Windows applications. You will also know how to incorporate these design standards into your WinUI applications.
To follow along with the examples in this chapter, the following software is required:
The source code for this chapter is available on GitHub at this URL: https://github.com/PacktPublishing/-Learn-WinUI-3.0/tree/master/Chapter07.
The Fluent Design System is a cross-platform system to guide developers in creating beautiful, intuitive applications. The website for Fluent Design (https://www.microsoft.com/design/fluent/) has dedicated pages with resources for developers on many platforms:
Fluent Design aims to be simple and intuitive. While it maintains its design philosophy across platforms, it also adapts aspects of its design to feel native on every platform. In Chapter 1, Introduction to WinUI, we discussed the origins of some of the current Fluent Design concepts in the Metro design that was introduced with Windows Phone. While the look and feel of Microsoft's designs have evolved over the years, some of the principles remain. The three core principles of the Fluent Design System are the following:
The driving philosophy behind the design is to adapt and feel natural. The device and the app should feel comfortable and anticipate the user's actions.
This is a very abstract and high-level explanation, so far. Let's explore the specifics of Fluent Design for Windows in the next section.
For Windows applications, Fluent Design encompasses several areas. When compared to other design systems, Fluent is more universal. Apple's Human Interface Guidelines (https://developer.apple.com/design/human-interface-guidelines/) have only been widely adopted on iOS and macOS. Google's Material Design (https://material.io/) system has seen wider adoption but only has toolkits available for Android, Flutter, and the web.
Fluent Design is most often compared with Material Design, as they share some concepts when it comes to shapes and texture, but Fluent Design uses transparency to much greater effect than Material Design. Choosing Fluent Design provides a rich toolset that you can use in WinUI and any other development platforms.
Let's explore some of these design aspects and how they apply to your WinUI applications.
A control equates to a single element of user input or interaction. We have already explored many of the controls available in WinUI in Chapter 5, Exploring WinUI Controls and Libraries. This is what some of the common WinUI controls look like in light and dark modes with the default accent color in Windows 10:
By default, the WinUI controls make use of Fluent styles. We will see how to override the default Fluent styles in our WinUI controls later in the chapter.
Patterns are groups of related controls or a group of controls that becomes a single new element. This group could be added to a composite control for re-use. Some examples of patterns in WinUI are the following:
Each of these patterns encapsulates the elements of Fluent Design to create a composite control that can be reused across applications. You may have control patterns in your projects that could be added to a shared control library for ease of reuse. Shared libraries like these can save time and ensure that good design practices are followed across teams.
The layout design is important to ensure that an application adapts to any screen size or orientation. Flexibility is a key tenet of a well-designed layout. When a page is resized, the contents can adapt by repositioning controls, adding/removing items, changing the flow of items, replacing controls with others that better fit the current available space, or simply resizing items. This is typically handled in XAML with Visual States. You can define a different VisualState for each size threshold to which your page must adapt, possibly defined as Narrow, Default, Wide, and ExtraWide. Each VisualState updates control properties to adapt to the new layout. Microsoft Docs has a great example of this at https://docs.microsoft.com/en-us/windows/uwp/design/layout/layouts-with-xaml. WinUI includes several different layout panels that can help developers create the right layout for their pages and respond to changes in size, orientation, and resolution.
There are Fluent recommendations for responding to user input. There are guidelines for reacting to the traditional mouse and keyboard input that developers have been handling for decades. Modern applications can do things such as pan, zoom, rotate, or scroll based on mouse input. A keyboard may be a physical keyboard or an onscreen keyboard for mobile and touch users.
Input can come in other forms with today's hardware:
User input can also be simulated with the input injection APIs. An example of where this might be useful is creating a Show Me How or Guided Tour feature in your app. Your code can execute some pre-defined steps, guiding the user through performing some action on the page. This API is beyond the scope of this book. To read about an example of using input injection to intercept mouse input and turn it into touch input, read this article on Microsoft Docs: https://docs.microsoft.com/en-us/windows/uwp/design/input/input-injection.
Style encompasses multiple aspects of Fluent Design:
These are just some of the aspects of style defined by Fluent Design. You can read more about them at Microsoft Docs: https://docs.microsoft.com/en-us/windows/uwp/design/style/.
Note
In 2021, Microsoft is revamping its UI styles in an effort codenamed Sun Valley. The new styles have a softer appearance with rounded corners. WinUI controls will be picking up these styles, and in fact, they are already available in WinUI 2.6. To read more about the new visual styles coming to WinUI, check out this blog post by Martin Zikmund: https://blog.mzikmund.com/2021/02/enabling-the-sun-valley-visual-styles-in-windows-ui-2-6-preview/.
Many of the aspects of Fluent style are made available to our WinUI apps via XAML styles and other static resources. Next, we will look at how we can update our sample application to respond to changes in a user's Windows theme.
It is time to incorporate a few of the Fluent Design principles into the application and polish the UI a little. Most of the WinUI controls are already designed to meet Fluent standards, but there were a few attributes we added without understanding Fluent Design.
Before we even get into the XAML to improve the styles, let's fix the application's title bar. Until now, the title bar always read MyMediaCollection without any spaces or indication of the current page:
Updating Display name will change the application's title bar text.
[assembly: AssemblyTitle("My Media Collection")]
[assembly: AssemblyDescription("Catalog and track your music, movies and books!")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("My Media Collection")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
public MainPage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Window.Current.Title = "Home";
}
Now, when you run the application, you should see the title bar text update as you navigate between the list of items and the item details. Let's make some changes to the styles of MainPage next.
Currently, the main page of our application doesn't have many styles. We set the FontWeight of a few TextBlock controls to Bold to set them apart as important items, but this doesn't follow the Fluent Design guidelines for typography. There is also a purple border separating the ListView header from its items:
Hardcoding colors is not a good practice. Even if you were using custom colors as product branding, you could centralize those in Application.Resources:
<StackPanel Orientation="Horizontal">
<SymbolIcon Symbol="Home" Margin="8"/>
<TextBlock Text="Home"
Style="{StaticResource SubheaderTextBlockStyle}"
Margin="8"/>
</StackPanel>
<TextBlock Text="Media Type:" Margin="4"
Style="{StaticResource SubtitleTextBlockStyle}"
VerticalAlignment="Bottom"/>
<StackPanel>
<StackPanel Orientation="Horizontal">
<SymbolIcon Symbol="Home" Margin="8"/>
<TextBlock Text="Home"
Style="{StaticResource SubheaderTextBlockStyle}"
Margin="8"/>
</StackPanel>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Right">
<TextBlock Text="Media Type:"
Margin="4"
Style="{StaticResource SubtitleTextBlockStyle}"
VerticalAlignment="Bottom"/>
<ComboBox ItemsSource="{x:Bind ViewModel.Mediums}"
SelectedItem="{x:Bind ViewModel.SelectedMedium, Mode=TwoWay}"
MinWidth="120"
Margin="0,2,6,4"
VerticalAlignment="Bottom"/>
</StackPanel>
</StackPanel>
<ListView.HeaderTemplate>
<DataTemplate>
<Grid Margin="4,0,4,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border BorderBrush="{ThemeResource SystemAccentColor}"
BorderThickness="0,0,0,1">
<TextBlock Text="Medium"
Margin="4,0,0,0"
Style="{StaticResource TitleTextBlockStyle}"/>
</Border>
<Border Grid.Column="1"
BorderBrush="{ThemeResource SystemAccentColor}"
BorderThickness="0,0,0,1">
<TextBlock Text="Title"
Margin="4,0,0,0"
Style="{StaticResource TitleTextBlockStyle}"/>
</Border>
</Grid>
</DataTemplate>
</ListView.HeaderTemplate>
<Border Grid.Row="2"
BorderBrush="{ThemeResource SystemAccentColor}"
BorderThickness="0,1,0,0"
Margin="4,0">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Right">
<Button Command="{x:Bind ViewModel.AddEditCommand}"
Content="Add/Edit Item" Margin="8,8,0,8"/>
<Button Command="{x:Bind ViewModel.DeleteCommand}"
Content="Delete Item"
Grid.Column="1"
Margin="8"/>
</StackPanel>
</Border>
Before moving on to the details page, let's see how the page looks if we select the dark mode in Windows. Open Windows Settings, go to Personalization | Colors, and select Dark from the Choose your color dropdown (if you normally use Dark, try changing it to Light):
Everything on the page switches to dark mode without any code changes. Great!
If you have a good reason to keep your application in light or dark mode, you can update Application.xaml to add a single attribute to the Application element using this command:
RequestedTheme="Dark"
This will apply the Dark theme to the entire application. If you have a reason to only force this theme on part of the application, the RequestedTheme attribute can be applied to a Page or Control. Now, let's apply the same types of styles to the details page.
We want to update ItemDetailsPage.xaml to make sure it has the same overall look and feel as the main page:
<StackPanel Orientation="Horizontal">
<SymbolIcon Symbol="Edit" Margin="8"/>
<TextBlock Text="Item Details"
Style="{StaticResource SubheaderTextBlockStyle}"
Margin="8"/>
</StackPanel>
<Grid Grid.Row="1"
BorderBrush="{ThemeResource SystemAccentColor}"
BorderThickness="0,1,0,1"
Margin="4,0,4,8">
That's all we need to change on this page. Run the application again and navigate to the details page to see how it looks:
This looks good. Now the styles of the pages match, and the added border lines match the color of the highlighted active input field.
Let's now shift gears and review a few tools that can help designers and developers when implementing Fluent Design.
We've seen how easy it is to pick up the default color and theme resources from the user's Windows settings, but what if you or your company wants to create a custom theme for an application. Maybe that theme needs to be shared across a suite of applications. You could create a XAML file with ResourceDictionary in Visual Studio and code all the markup by hand for a new style. Visual Studio's IntelliSense will help in some regards. However, there is an easier way.
Microsoft has created an open source tool called the Fluent XAML Theme Editor. It is available on the Windows Store at https://www.microsoft.com/en-us/p/fluent-xaml-theme-editor/9n2xd3q8x57c, and the source is available on GitHub here: https://github.com/Microsoft/fluent-xaml-theme-editor. This application provides an easy-to-use visual designer to create a ResourceDictionary XAML that you can drop into your projects.
Note
The Fluent XAML Theme Editor adjusts the styles for the built-in UWP controls, but these same styles will work with our WinUI controls as well.
To install the application, open the Microsoft Store app, search for fluent xaml in the search field, and you will find Fluent XAML Theme Editor in the search results. Click it in the search results to view the product page:
If you have the app installed, there will be a Launch button. If it is not installed, you can click the Install button. When it completes its installation, you will find the app in your Start Menu.
When you first launch the app, it will launch with the default style, displayed in both light and dark themes. On the right-hand panel, you will find controls for changing the colors, shapes, and typography (coming soon):
On the Colors tab, you can select one of the default profiles from the Color Presets dropdown. In addition to the default preset, there is Lavender, Forest, and Nighttime. There are also options to load additional presets or save your current color settings to a new preset. These color presets are saved in JSON format.
Note
Any colors you specify here will override the Windows system accent color that will get picked up by WinUI applications by default. Unless your application has a good reason to follow another theme, it is usually best to let WinUI use the user's chosen accent colors. Designing a custom theme should be undertaken by an experienced design team.
Clicking on any of the colors in the current preset will launch a color picker window where you can adjust the current color:
The Region, Base, and Primary colors can be adjusted for light and dark themes independently.
The Shapes panel provides controls to adjust the Corner Radius of the Controls and the Overlay, as well as the default Border Thickness for the theme.
Shape presets can also be saved and loaded. The app comes with two presets: Default and No Rounding, Thicker Borders. The difference is subtle but noticeable:
When you are done adjusting the color and shape settings, use the Export button to open a new window containing a ResourceDictionary with your theme data. You can copy the XAML and paste it into a Resources section in your project:
Next, let's look at a great way to find and discover resources you can use in your WinUI app.
Another great tool to help WinUI developers while they are styling their application is the UWP Resources Gallery. While this tool does have UWP and not WinUI in its name, all the resources you can discover in the tool also exist in WinUI 3.0. This app is open source and is available on the Windows Store at https://www.microsoft.com/en-us/p/uwp-resources-gallery/9pjjl433vx9r. A WinUI developer named Marcel Wagner maintains the open source project on GitHub at https://github.com/chingucoding/UWP-Resources-Gallery.
After installing the app, you will see that there several are types of resources you can browse or search:
Now, we will take a quick look at some additional Fluent Design tools geared toward designers.
While a deep dive into user interface design is beyond the scope of this book, we will briefly review some of the Fluent Design toolkits available for popular design tools. The Fluent Design toolkits for all of these tools can be downloaded from Microsoft Docs at https://docs.microsoft.com/en-us/windows/uwp/design/downloads/:
The Fluent Design toolkit for Photoshop includes several PSD files. It is also possible to work with PSD files in free image editors such as GIMP (https://www.gimp.org/) or Paint.NET (https://www.getpaint.net/). Paint.NET requires an open source plugin, available at https://www.psdplugin.com/.
We have learned a lot about Fluent Design, design resources, and the tools available to WinUI developers in this chapter. You will be able to use these tools and techniques in your WinUI application design or recommend them to designers at your company. We also updated the My Media Collection app to be more compliant with Fluent Design recommendations.
In the next chapter, we will examine the Windows Control Toolkit, an open source bundle of Windows controls for WinUI, UWP, WPF, and WinForms.
3.16.66.156