From 197a8ae12049e7e9c5a159c0000828e573a0429f Mon Sep 17 00:00:00 2001 From: Laszlo Nemeth <57342539+donlaci@users.noreply.github.com> Date: Thu, 7 Dec 2023 10:57:30 +0100 Subject: [PATCH] [ImageResizer] Workaround for exception in wpf ui. (#30225) * [ImageResizer] Workaround for exception in wpf ui. * Adding try catch to the other 3 places the SystemThemeWatcher is used. Rearranging code in Image Resizer, which might solve the issue. --- src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs | 12 +++++++++++- .../imageresizer/ui/Views/MainWindow.xaml.cs | 14 ++++++++++++-- .../launcher/PowerLauncher/MainWindow.xaml.cs | 12 ++++++++++-- .../poweraccent/PowerAccent.UI/Selector.xaml.cs | 13 ++++++++++++- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs b/src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs index 60ec8b3a3a..cf516f9959 100644 --- a/src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs +++ b/src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs @@ -49,7 +49,17 @@ public partial class OCROverlay : Window Top = screenRectangle.Top >= 0 ? screenRectangle.Top : screenRectangle.Top + (screenRectangle.Height / 2); InitializeComponent(); - Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this, Wpf.Ui.Controls.WindowBackdropType.None); + + // workaround for #30177 + try + { + Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this, Wpf.Ui.Controls.WindowBackdropType.None); + } + catch (Exception ex) + { + Logger.LogError($"Exception in SystemThemeWatcher.Watch, issue 30177. {ex.Message}"); + } + PopulateLanguageMenu(); } diff --git a/src/modules/imageresizer/ui/Views/MainWindow.xaml.cs b/src/modules/imageresizer/ui/Views/MainWindow.xaml.cs index 9a6dfc15b1..d9d75abb11 100644 --- a/src/modules/imageresizer/ui/Views/MainWindow.xaml.cs +++ b/src/modules/imageresizer/ui/Views/MainWindow.xaml.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using Common.UI; using ImageResizer.ViewModels; +using ManagedCommon; using Microsoft.Win32; using Wpf.Ui.Controls; using AppResources = ImageResizer.Properties.Resources; @@ -18,7 +19,8 @@ namespace ImageResizer.Views public MainWindow(MainViewModel viewModel) { DataContext = viewModel; - Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this); + + InitializeComponent(); if (OSVersionHelper.IsWindows11()) { @@ -29,7 +31,15 @@ namespace ImageResizer.Views WindowBackdropType = WindowBackdropType.None; } - InitializeComponent(); + // workaround for #30177 + try + { + Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this, WindowBackdropType); + } + catch (Exception ex) + { + Logger.LogError($"Exception in SystemThemeWatcher.Watch, issue 30177. {ex.Message}"); + } } public IEnumerable OpenPictureFiles() diff --git a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs index 418d8f4883..9c84498354 100644 --- a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs +++ b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs @@ -56,8 +56,6 @@ namespace PowerLauncher _settings = settings; InitializeComponent(); - Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this); - if (OSVersionHelper.IsWindows11()) { WindowBackdropType = Wpf.Ui.Controls.WindowBackdropType.Acrylic; @@ -67,6 +65,16 @@ namespace PowerLauncher WindowBackdropType = Wpf.Ui.Controls.WindowBackdropType.None; } + // workaround for #30217 + try + { + Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this, WindowBackdropType); + } + catch (Exception ex) + { + Log.Exception("Exception in SystemThemeWatcher.Watch, issue 30217.", ex, GetType()); + } + _firstDeleteTimer.Elapsed += CheckForFirstDelete; _firstDeleteTimer.Interval = 1000; NativeEventWaiter.WaitForEventLoop( diff --git a/src/modules/poweraccent/PowerAccent.UI/Selector.xaml.cs b/src/modules/poweraccent/PowerAccent.UI/Selector.xaml.cs index 14df70a72f..d7fab64210 100644 --- a/src/modules/poweraccent/PowerAccent.UI/Selector.xaml.cs +++ b/src/modules/poweraccent/PowerAccent.UI/Selector.xaml.cs @@ -5,6 +5,7 @@ using System; using System.ComponentModel; using System.Windows; +using ManagedCommon; using Wpf.Ui.Controls; using Point = PowerAccent.Core.Point; using Size = PowerAccent.Core.Size; @@ -38,7 +39,17 @@ public partial class Selector : FluentWindow, IDisposable, INotifyPropertyChange public Selector() { InitializeComponent(); - Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this); + + // workaround for #30177 + try + { + Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this); + } + catch (Exception ex) + { + Logger.LogError($"Exception in SystemThemeWatcher.Watch, issue 30177. {ex.Message}"); + } + Application.Current.MainWindow.ShowActivated = false; Application.Current.MainWindow.Topmost = true; }