5
5
using Microsoft . UI . Windowing ;
6
6
using Microsoft . UI . Xaml ;
7
7
using Microsoft . UI . Xaml . Controls ;
8
+ using Microsoft . UI . Xaml . Media ;
8
9
using Microsoft . UI . Xaml . Media . Animation ;
9
10
using Microsoft . Windows . ApplicationModel . Resources ;
11
+ using System . Collections . Concurrent ;
10
12
using System . IO ;
11
13
using Windows . ApplicationModel ;
12
14
using Windows . Graphics ;
@@ -38,6 +40,8 @@ public static string LogoPath
38
40
public static nint GetWindowHandle ( Window w )
39
41
=> WinRT . Interop . WindowNative . GetWindowHandle ( w ) ;
40
42
43
+ private static BlockingCollection < WinUIEx . WindowEx > WindowCache = new ( ) ;
44
+
41
45
/// <summary>
42
46
/// Open properties window
43
47
/// </summary>
@@ -97,17 +101,21 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
97
101
RequestedTheme = ThemeHelper . RootTheme
98
102
} ;
99
103
100
- var propertiesWindow = new WinUIEx . WindowEx
104
+ WinUIEx . WindowEx propertiesWindow ;
105
+ if ( ! WindowCache . TryTake ( out propertiesWindow ! ) )
101
106
{
102
- IsMinimizable = false ,
103
- IsMaximizable = false ,
104
- MinWidth = 460 ,
105
- MinHeight = 550 ,
106
- Width = 800 ,
107
- Height = 550 ,
108
- Content = frame ,
109
- Backdrop = new WinUIEx . MicaSystemBackdrop ( ) ,
110
- } ;
107
+ propertiesWindow = new ( ) ;
108
+ propertiesWindow . Closed += PropertiesWindow_Closed ;
109
+ }
110
+
111
+ propertiesWindow . IsMinimizable = false ;
112
+ propertiesWindow . IsMaximizable = false ;
113
+ propertiesWindow . MinWidth = 460 ;
114
+ propertiesWindow . MinHeight = 550 ;
115
+ propertiesWindow . Width = 800 ;
116
+ propertiesWindow . Height = 550 ;
117
+ propertiesWindow . Content = frame ;
118
+ propertiesWindow . SystemBackdrop = new MicaBackdrop ( ) ;
111
119
112
120
var appWindow = propertiesWindow . AppWindow ;
113
121
appWindow . Title = "Properties" . GetLocalizedResource ( ) ;
@@ -128,8 +136,6 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
128
136
} ,
129
137
new SuppressNavigationTransitionInfo ( ) ) ;
130
138
131
- appWindow . Show ( ) ;
132
-
133
139
// WINUI3: Move window to cursor position
134
140
InteropHelpers . GetCursorPos ( out var pointerPosition ) ;
135
141
var displayArea = DisplayArea . GetFromPoint ( new PointInt32 ( pointerPosition . X , pointerPosition . Y ) , DisplayAreaFallback . Nearest ) ;
@@ -142,6 +148,31 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
142
148
} ;
143
149
144
150
appWindow . Move ( appWindowPos ) ;
151
+
152
+ appWindow . Show ( ) ;
153
+ }
154
+
155
+ // Destruction of Window objects seems to cause access violation. (#12057)
156
+ // So instead of destroying the Window object, cache it and reuse it as a workaround.
157
+ private static void PropertiesWindow_Closed ( object sender , WindowEventArgs args )
158
+ {
159
+ if ( ! App . AppModel . IsMainWindowClosed && sender is WinUIEx . WindowEx window )
160
+ {
161
+ args . Handled = true ;
162
+
163
+ window . AppWindow . Hide ( ) ;
164
+ window . Content = null ;
165
+ WindowCache . Add ( window ) ;
166
+ }
167
+ }
168
+
169
+ public static void DestroyCachedWindows ( )
170
+ {
171
+ while ( WindowCache . TryTake ( out var window ) )
172
+ {
173
+ window . Closed -= PropertiesWindow_Closed ;
174
+ window . Close ( ) ;
175
+ }
145
176
}
146
177
}
147
178
}
0 commit comments