77using CefSharp . Wpf . Rendering ;
88using Microsoft . Win32 . SafeHandles ;
99using System ;
10- using System . Collections . Generic ;
1110using System . Threading ;
1211using System . Threading . Tasks ;
1312using System . Windows ;
@@ -23,8 +22,6 @@ namespace CefSharp.Wpf
2322{
2423 public class ChromiumWebBrowser : ContentControl , IRenderWebBrowser , IWpfWebBrowser
2524 {
26- private readonly List < IDisposable > disposables = new List < IDisposable > ( ) ;
27-
2825 private HwndSource source ;
2926 private HwndSourceHook sourceHook ;
3027 private DispatcherTimer tooltipTimer ;
@@ -118,6 +115,7 @@ public ChromiumWebBrowser()
118115 Dispatcher . BeginInvoke ( ( Action ) ( ( ) => WebBrowser = this ) ) ;
119116
120117 Loaded += OnLoaded ;
118+ SizeChanged += OnActualSizeChanged ;
121119
122120 GotKeyboardFocus += OnGotKeyboardFocus ;
123121 LostKeyboardFocus += OnLostKeyboardFocus ;
@@ -154,9 +152,6 @@ public ChromiumWebBrowser()
154152
155153 managedCefBrowserAdapter = new ManagedCefBrowserAdapter ( this , true ) ;
156154
157- disposables . Add ( new DisposableEventWrapper ( this , ActualHeightProperty , OnActualSizeChanged ) ) ;
158- disposables . Add ( new DisposableEventWrapper ( this , ActualWidthProperty , OnActualSizeChanged ) ) ;
159-
160155 ResourceHandlerFactory = new DefaultResourceHandlerFactory ( ) ;
161156 BrowserSettings = new BrowserSettings ( ) ;
162157
@@ -205,6 +200,7 @@ protected virtual void Dispose(bool isdisposing)
205200
206201 // Release internal event listeners:
207202 Loaded -= OnLoaded ;
203+ SizeChanged -= OnActualSizeChanged ;
208204 GotKeyboardFocus -= OnGotKeyboardFocus ;
209205 LostKeyboardFocus -= OnLostKeyboardFocus ;
210206
@@ -232,11 +228,6 @@ protected virtual void Dispose(bool isdisposing)
232228 managedCefBrowserAdapter = null ;
233229 }
234230
235- foreach ( var disposable in disposables )
236- {
237- disposable . Dispose ( ) ;
238- }
239- disposables . Clear ( ) ;
240231
241232 browserInitialized = false ;
242233 UiThreadRunAsync ( ( ) =>
@@ -890,9 +881,9 @@ private void RemoveSourceHook()
890881 }
891882 }
892883
893- private void CreateOffscreenBrowserWhenActualSizeChanged ( )
884+ private void CreateOffscreenBrowserWhenActualSizeChanged ( Size newSize )
894885 {
895- if ( browserCreated || System . ComponentModel . DesignerProperties . GetIsInDesignMode ( this ) )
886+ if ( browserCreated || System . ComponentModel . DesignerProperties . GetIsInDesignMode ( this ) || newSize . IsEmpty )
896887 {
897888 return ;
898889 }
@@ -917,10 +908,10 @@ private void UiThreadRunAsync(Action action, DispatcherPriority priority = Dispa
917908 }
918909 }
919910
920- private void OnActualSizeChanged ( object sender , EventArgs e )
911+ private void OnActualSizeChanged ( object sender , SizeChangedEventArgs e )
921912 {
922913 // Initialize RenderClientAdapter when WPF has calculated the actual size of current content.
923- CreateOffscreenBrowserWhenActualSizeChanged ( ) ;
914+ CreateOffscreenBrowserWhenActualSizeChanged ( e . NewSize ) ;
924915
925916 if ( browser != null )
926917 {
0 commit comments