Skip to content

Commit 82b734d

Browse files
committed
Merge pull request #291 from JanEggers/WebViewCleanup
Improved WebView resource cleanup
2 parents fd20a06 + a6bf79e commit 82b734d

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

CefSharp.Wpf/WebView.cs

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class WebView : ContentControl, IRenderWebBrowser, IWpfWebBrowser
3131
private Image image;
3232
private Image popupImage;
3333
private Popup popup;
34-
private static readonly List<IDisposable> disposables = new List<IDisposable>();
34+
private readonly List<IDisposable> disposables = new List<IDisposable>();
3535

3636
public BrowserSettings BrowserSettings { get; set; }
3737
public bool IsBrowserInitialized { get; private set; }
@@ -83,9 +83,9 @@ public string Address
8383

8484
public static readonly DependencyProperty AddressProperty =
8585
DependencyProperty.Register("Address", typeof(string), typeof(WebView),
86-
new UIPropertyMetadata(null, OnAdressChanged));
86+
new UIPropertyMetadata(null, OnAddressChanged));
8787

88-
private static void OnAdressChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
88+
private static void OnAddressChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
8989
{
9090
WebView owner = (WebView)sender;
9191
string oldValue = (string)args.OldValue;
@@ -144,6 +144,65 @@ public string Title
144144
public static readonly DependencyProperty TitleProperty =
145145
DependencyProperty.Register("Title", typeof(string), typeof(WebView), new PropertyMetadata(defaultValue: null));
146146

147+
#endregion CleanupElement dependency property
148+
149+
#region CleanupElement dependency property
150+
151+
/// <summary>
152+
/// The CleanupElement Controls when the BrowserResources will be cleand up.
153+
/// The WebView will register on Unloaded of the provided Element and dispose all resources when that handler is called.
154+
/// By default the cleanup element is the Window that contains the WebView.
155+
/// if you want cleanup to happen earlier provide another FrameworkElement.
156+
/// Be aware that this Control is not usable anymore after cleanup is done.
157+
/// </summary>
158+
/// <value>
159+
/// The cleanup element.
160+
/// </value>
161+
public FrameworkElement CleanupElement
162+
{
163+
get { return (FrameworkElement)GetValue(CleanupElementProperty); }
164+
set { SetValue(CleanupElementProperty, value); }
165+
}
166+
167+
public static readonly DependencyProperty CleanupElementProperty =
168+
DependencyProperty.Register("CleanupElement", typeof(FrameworkElement), typeof(WebView), new PropertyMetadata(null, OnCleanupElementChanged));
169+
170+
private static void OnCleanupElementChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
171+
{
172+
var owner = (WebView)sender;
173+
var oldValue = (FrameworkElement)args.OldValue;
174+
var newValue = (FrameworkElement)args.NewValue;
175+
176+
owner.OnCleanupElementChanged(oldValue, newValue);
177+
}
178+
179+
protected virtual void OnCleanupElementChanged(FrameworkElement oldValue, FrameworkElement newValue)
180+
{
181+
if (oldValue != null)
182+
{
183+
oldValue.Unloaded -= OnCleanupElementUnloaded;
184+
}
185+
186+
if (newValue != null)
187+
{
188+
newValue.Unloaded -= OnCleanupElementUnloaded;
189+
newValue.Unloaded += OnCleanupElementUnloaded;
190+
}
191+
}
192+
193+
private void OnCleanupElementUnloaded(object sender, RoutedEventArgs e)
194+
{
195+
Cleanup();
196+
}
197+
198+
protected virtual void Cleanup()
199+
{
200+
foreach (var disposable in disposables)
201+
{
202+
disposable.Dispose();
203+
}
204+
}
205+
147206
#endregion Title dependency property
148207

149208
#region TooltipText dependency property
@@ -277,10 +336,6 @@ private static void OnApplicationExit(object sender, ExitEventArgs e)
277336
// should not explicitly have to perform this.
278337
if (Cef.IsInitialized)
279338
{
280-
foreach (var disposable in disposables)
281-
{
282-
disposable.Dispose();
283-
}
284339
GC.Collect();
285340
GC.WaitForPendingFinalizers();
286341

@@ -290,10 +345,11 @@ private static void OnApplicationExit(object sender, ExitEventArgs e)
290345

291346
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
292347
{
348+
CleanupElement = Window.GetWindow(this);
293349
AddSourceHookIfNotAlreadyPresent();
294350
}
295351

296-
public void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
352+
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
297353
{
298354
RemoveSourceHook();
299355
}

0 commit comments

Comments
 (0)