lepoco / wpfui

WPF UI provides the Fluent experience in your known and loved WPF framework. Intuitive design, themes, navigation and new immersive controls. All natively and effortlessly.
https://wpfui.lepo.co
MIT License
7.69k stars 750 forks source link

Unhandled File not found 0x80070002 in DwmGetColorizationParameters #593

Open wfjsw opened 1 year ago

wfjsw commented 1 year ago

Describe the bug

The DWM api is throwing a System.IO.FileNotFoundException when accessing Dwmapi.DwmGetColorizationParameters(out DWMCOLORIZATIONPARAMS dwParameters)

To Reproduce

Unknown

Expected behavior

The error is caught and a default color is applied

Screenshots

No response

OS version

Windows 10.0.22621

.NET version

.NET 6.0.16

WPF-UI NuGet version

https://github.com/lepoco/wpfui/commit/91a708991a69f168943e8d961cc123bf8ca92965

Additional context

System.IO.FileNotFoundException: 系统找不到指定的文件。 (0x80070002)
  ?, in void Dwmapi.DwmGetColorizationParameters(out DWMCOLORIZATIONPARAMS dwParameters)
  ?, in Color UnsafeNativeMethods.GetDwmColor()
  ?, in void Theme.Apply(ThemeType themeType, WindowBackdropType backgroundEffect, bool updateAccent, bool forceBackground)
  ?, in void Watcher.UpdateThemes(SystemThemeType systemTheme)
  ?, in void Watcher.Watch(Window window, WindowBackdropType backgroundEffect, bool updateAccents, bool forceBackground)+(object sender, RoutedEventArgs args) => { }
  ?, in void RoutedEventHandlerInfo.InvokeHandler(object target, RoutedEventArgs routedEventArgs)
  ?, in void EventRoute.InvokeHandlersImpl(object source, RoutedEventArgs args, bool reRaised)
  ?, in void UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
  ?, in void UIElement.RaiseEvent(RoutedEventArgs e)
  ?, in void BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
  ?, in object BroadcastEventHelper.BroadcastLoadedEvent(object root)
  ?, in void MediaContext.FireLoadedPendingCallbacks()
  ?, in void MediaContext.FireInvokeOnRenderCallbacks()
  ?, in void MediaContext.RenderMessageHandlerCore(object resizedCompositionTarget)
  ?, in object MediaContext.RenderMessageHandler(object resizedCompositionTarget)
  ?, in void MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
  ?, in void HwndTarget.OnResize()
  ?, in IntPtr HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
  ?, in IntPtr HwndSource.HwndTargetFilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  ?, in IntPtr HwndWrapper.WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  ?, in object HwndSubclass.DispatcherCallbackOperation(object o)
  ?, in object ExceptionWrapper.InternalRealCall(Delegate callback, object args, int numArgs)
  ?, in object ExceptionWrapper.TryCatchWhen(object source, Delegate callback, object args, int numArgs, Delegate catchHandler)
ChainsLunatic commented 1 year ago

The exception does not occur if you pass false to Theme.Apply's updateAccent. So a possible workaround would be to try passing true and retry with passing false on FileNotFoundException, e.g.

private void ApplyTheme()
{
    try
    {
        Theme.Apply(_settings.DarkMode ? ThemeType.Dark : ThemeType.Light, WindowBackdropType, _updateAccentColor);
    }
    catch (FileNotFoundException)
    {
        if (_updateAccentColor)
        {
            _updateAccentColor = false;
            ApplyTheme();
        }
    }
}

Another way would be to determine the actual Windows version the error occurs on and implement a check for it's version number. This could probably be introduced into the actual code base also. I sadly can't do that as I got no systems to test on.

ilyfairy commented 1 year ago

There may be a problem with windows. When calling DwmGetColorizationParameters, you will encounter a 0x80070002 exception. I tried changing the theme color in settings and the problem was solved

YiLai-Qube commented 11 months ago

There may be a problem with windows. When calling DwmGetColorizationParameters, you will encounter a 0x80070002 exception. I tried changing the theme color in settings and the problem was solved

Changing the colour theme and default windows mode (light/dark) in windows settings solves the problem for me

davidegiacometti commented 11 months ago

I am randomly experiencing this issue while turning on/off high contrast or switching between high contrast modes Windows 10 22H2