dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.08k stars 1.17k forks source link

App crashes when high contrast mode is enabled in Windows in .NET 9 with fluent themes enabled #10043

Open aquinn39 opened 1 week ago

aquinn39 commented 1 week ago

Description

When enabling fluent themes in WPF in .NET 9 and high contrast mode is on in Windows, or fluent themes are already enabled and then high contrast mode is enabled, the app will crash with a System.Windows.Markup.XamlParseException. The full exception is:

System.Windows.Markup.XamlParseException: 'Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.'

ArgumentException: Item has already been added. Key in dictionary: 'SystemColorWindowColorBrush'  Key being added: 'SystemColorWindowColorBrush'

Image

Reproduction Steps

  1. Create a new WPF project and set it to use .NET 9
  2. Enable fluent themes in the project as described here: https://github.com/dotnet/core/blob/main/release-notes/9.0/preview/preview4/wpf.md#support-for-windows-11-theming
  3. Enable high contrast mode in Windows
  4. Build and run the app
  5. Observe that it crashes
  6. Alternatively, swap step 3 and 4 around

Expected behavior

The app should not crash.

Actual behavior

The app crashes.

Regression?

Yes, not an issue with the old themes and older versions of .NET nor .NET Framework.

Known Workarounds

Enable the fluent themes using the Application.ThemeMode property within a try catch. App will still crash once the themes are enabled and the user turns on high contrast mode, but not if it was already on.

Impact

WPF cannot be used with the fluent themes while high contrast mode is enabled on the system.

Configuration

.NET 9, AnyCPU, Windows 11 build 22631.4037.

Other information

No response

aquinn39 commented 2 days ago

Still happens in .NET 9 😬. Honestly not sure how it got released in this state...

dipeshmsft commented 2 days ago

@aquinn39, it got messed up in one of the last minute fixes that I was doing, I have prepared a fix for it and will push it ASAP.

aquinn39 commented 2 days ago

@dipeshmsft Ahh okay, will the fix come out soon or does it need to wait for the next .NET 9 patch?

dipeshmsft commented 1 day ago

@aquinn39 I am not sure about this right now, I will take a look at it get back to you.