Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

What's new in WPF for .NET 9

Feedback

In this article

This article describes what's new in Windows Presentation Foundation (WPF) for .NET 9. The main area of focus for WPF this year was improving the visual capabilities of WPF and providing a new theme based on the Fluent design principles for Windows 11.

You can preview the new theme by downloading theWPF Gallery app from theMicrosoft Store.

Fluent theme

A new theme is included with WPF that delivers a fresh, modern Windows 11 aesthetic for WPF apps. It includes integrated light and dark modes, and a system accent color support.

  • Fluent theme in light mode:

    A screenshot of the WPF Gallery app, demonstrating the fluent theme in light mode.

  • Fluent theme in dark mode:

    A screenshot of the WPF Gallery app, demonstrating the fluent theme in dark mode

Apply the theme

You can apply the Fluent theme in two ways, setting theThemeMode property or referencing the Fluent theme resource dictionary. For more information about the theme mode setting, seeThemeMode.

The Fluent theme resource dictionary is available at the following pack URI:/PresentationFramework.Fluent;component/Themes/Fluent.xaml. To apply the resource at the application-level, load the resource into your app's resources:

<Application.Resources>    <ResourceDictionary>        <ResourceDictionary.MergedDictionaries>            <ResourceDictionary Source="pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml" />        </ResourceDictionary.MergedDictionaries>    </ResourceDictionary></Application.Resources>

The resource dictionary can also be applied to aWindow to theme just the window itself.

ThemeMode

A new styling API has been added to WPF, which is exposed through theThemeMode property. By using this property, you can apply the Fluent style without having to reference a styling resource dictionary directly.

Available values are:

  • Light—Applies the light Fluent theme.
  • Dark—Applies the dark Fluent theme.
  • System—Applies either the light or dark Fluent theme, based on the user's current Windows setting.
  • None—(default) Uses the Aero2 theme.

To apply a theme mode for the whole application, set theThemeMode property on theApplication type. To apply it to a single window, setThemeMode on theWindow type.

For example, style the entire application based on the current light or dark theme set by Windows:

<Application x:Class="MyWpfProject.App"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:local="clr-namespace:MyWpfProject"             StartupUri="MainWindow.xaml"             ThemeMode="System">

Here's an example of forcing the light theme, regardless of the theme set by Windows:

<Window x:Class="MyWpfProject.LightWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:local="clr-namespace:MyWpfProject"        Title="LightWindow" Height="450" Width="800"        ThemeMode="Light">

If theThemeMode is set to any value other thanNone at the application-level,None can no longer be applied at the window-level.

ThemeMode is designed to respect the settings set by a Fluent Dictionary, allowing you to customize the Fluent theme.

Set in code

Support for changing setting theThemeMode in code is currently an experimental feature. Accessing theThemeMode property in code generates errorWPF0001, preventing access to the API. Suppress the error to access to the API.

Warning

This API is experimental and subject to change.

First, add the followingPropertyGroup element to your project file to suppress the error:

<PropertyGroup>    <NoWarn>$(NoWarn);WPF0001</NoWarn></PropertyGroup>

Tip

You can use the#pragma warning disable WPF0001 directive to suppress the error where it occurs instead of disabling it for the entire project.

Next, set either theThemeMode property at the application-level or window-level:

// Set light mode at the application-levelApplication.Current.ThemeMode = ThemeMode.Light;// Set dark mode on the current windowthis.ThemeMode = ThemeMode.Dark;

Support for Windows accent color

Windows 10 introduced a user-selectable accent color that's used in providing a personal touch or calling out a specific visual element. WPF now supports the user-selected accent color.

The visual color is available as aSystem.Windows.Media.Color,System.Windows.Media.SolidColorBrush, orSystem.Windows.ResourceKey. Along with the color itself, light and dark shades of the accent color are available. These are accessed throughSystem.Windows.SystemColors:

ColorColor Resource KeyBrushBrush Resource Key
AccentAccentColorAccentColorKeyAccentColorBrushAccentColorBrushKey
Light 1AccentColorLight1AccentColorLight1KeyAccentColorLight1BrushAccentColorLight1BrushKey
Light 2AccentColorLight2AccentColorLight2KeyAccentColorLight2BrushAccentColorLight2BrushKey
Light 3AccentColorLight3AccentColorLight3KeyAccentColorLight3BrushAccentColorLight3BrushKey
Dark 1AccentColorDark1AccentColorDark1KeyAccentColorDark1BrushAccentColorDark1BrushKey
Dark 2AccentColorDark2AccentColorDark2KeyAccentColorDark2BrushAccentColorDark2BrushKey
Dark 3AccentColorDark3AccentColorDark3KeyAccentColorDark3BrushAccentColorDark3BrushKey

Important

Accent colors are available with or without the Fluent theme.

When creating a UI that uses the accent color, wrap the resource key in a dynamic resource. When a user changes the accent color while the app is opened, the color is updated automatically in the app. For example, here's aTextBlock with the foreground color set to the user's chosen accent color:

<TextBlock Text="First Name:"           Foreground="{DynamicResource {x:Static SystemColors.AccentColorBrushKey}}" />

Hyphen-based ligature support

WPF has never supported hyphen-based ligatures in UI controls such as theTextBlock. This long-standing community ask was added in .NET 9.

Here's an image of the ligatures not being applied to the glyphs in .NET 8:

A screenshot of a simple WPF app that has a text block showing how glyphs aren't combined into ligatures with .NET 8.

And now, that same text as rendered in .NET 9:

A screenshot of a simple WPF app that has a text block showing how glyphs are combined into ligatures with .NET 9.

BinaryFormatter no longer supported

BinaryFormatter is considered unsafe because it's vulnerable to deserialization attacks, which can lead to denial of service (DoS), information disclosure, or remote code execution. It was implemented before deserialization vulnerabilities were well understood, and its design doesn't follow modern security best practices.

Starting with .NET 9, its implementation has been removed to prevent these security risks. WhenBinaryFormatter is used, thePlatformNotSupportedException exception is thrown.

WPF usedBinaryFormatter in many scenarios, such as when serializing data for clipboard and drag-and-drop operations. Internally, WPF continues to use a safer subset ofBinaryFormatter to handle specific use cases with a known set of types.

For more information aboutBinaryFormatter, seeWPF migration guide for BinaryFormatter.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?