question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Support setting/changing Application.Current.RequestedTheme after app constructor (rather than throw exception)

See original GitHub issue

Describe the bug Setting Application.Current.RequestedTheme at run-time to alter the app’s light/dark theme throws this exception:

System.NotSupportedException
  HResult=0x80131515
  Message=Specified method is not supported.
  Source=WinRT.Runtime
  StackTrace:
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Microsoft.UI.Xaml.IApplication.global::Microsoft.UI.Xaml.IApplication.set_RequestedTheme(ApplicationTheme value)
   at Microsoft.UI.Xaml.Application.set_RequestedTheme(ApplicationTheme value)
   at TestApp.RootWindow.OnThemeMenuFlyoutItemClick(Object sender, RoutedEventArgs e) in S:\Code\TestApp\Views\RootWindow.xaml.cs:line 71
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.<>c__DisplayClass10_0.<Do_Abi_Invoke>b__0(RoutedEventHandler invoke)
   at WinRT.ComWrappersSupport.MarshalDelegateInvoke[T](IntPtr thisPtr, Action`1 invoke)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

Setting the RequestedTheme property on the root Grid in my Window succeeds. But I want to change it app-wide, not only on this window.

Steps to reproduce the bug

Use this code to reproduce:

Application.Current.RequestedTheme = ApplicationTheme.Dark;

Expected behavior

No exception and the app theme should toggle to the selected theme.

Version Info

NuGet package version: [Microsoft.WinUI 3.0.0-preview4.210210.4]

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
billhenncommented, Mar 11, 2021

@codendone Thanks, I apologize I didn’t see that remark before. The more descriptive error message would certainly help prevent confusion on this, and as you said, error messages really need to be more detailed across the board.

I would really ask that you improve this to support app-wide theme changes without having to restart the app. All popular modern apps support instant live theme changes between variations of light and dark themes without having to restart, and it’s kind of an expected app capability at this point. Not having support for that in a “modern” UI platform is very disappointing. Thanks for listening!

2reactions
jtorjocommented, Jul 5, 2022

Guys, can you please please please add just a tad of description in the exception, when you throw it? I wanted to bang my head against the wall on this one (Until i figured out i should set that in the constructor). The (insanely generic) exception is beyond annoying.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting the theme of the application during execution just ...
RequestedTheme = value” works well. When I use “Frame.Navigate()” to navigate to other pages, the theme still changes. I can't reproduce your ...
Read more >
c# - Throwing exception from constructor?
Throwing exceptions in constructors in C# is fine, but a constructor should always create a valid object. I prefer to keep construction ...
Read more >
CA1032: Implement standard exception constructors
Exception types must implement the following three public constructors: public NewException(). public NewException(string).
Read more >
Can Constructors Throw Exceptions in Java
The short answer is yes! Of course, properly implementing exceptions in your constructors is essential to getting the best results.
Read more >
Crash of application if exception is thrown in constructor
I have experienced crash of application in case exception is thrown inside constructor of the class in release version of project with /O1 ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found