Skip to content

Commit df19cef

Browse files
committed
Free webview2 control resources when page is unloaded.
1 parent 0b7d035 commit df19cef

16 files changed

+94
-32
lines changed

SampleApps/webview2_sample_uwp/Package.appxmanifest

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
<Capability Name="privateNetworkClientServer" />
5151
<uap:Capability Name="enterpriseAuthentication"/>
5252
<uap:Capability Name="sharedUserCertificates"/>
53-
<rescap:Capability Name="enterpriseCloudSSO" />
5453
<DeviceCapability Name="location"/>
5554
<DeviceCapability Name="microphone"/>
5655
<DeviceCapability Name="webcam"/>

SampleApps/webview2_sample_uwp/Pages/AddHostObject.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
Use of this source code is governed by a BSD-style license that can be
33
found in the LICENSE file.-->
44

5-
<Page
5+
<local:BasePage
66
x:Class="WebView2_UWP.Pages.AddHostObject"
77
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
88
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
9-
xmlns:local="using:WebView2_UWP.Pages"
109
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
1110
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1211
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
12+
xmlns:local="using:WebView2_UWP.Pages"
1313
mc:Ignorable="d"
1414
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1515

@@ -77,5 +77,5 @@ found in the LICENSE file.-->
7777

7878
<controls:WebView2 Grid.Column="1" x:Name="WebView2" />
7979
</Grid>
80-
</Page>
80+
</local:BasePage>
8181

SampleApps/webview2_sample_uwp/Pages/AddHostObject.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace WebView2_UWP.Pages
1313
{
14-
public sealed partial class AddHostObject : Page
14+
public sealed partial class AddHostObject : BasePage
1515
{
1616
private Bridge _bridge;
1717

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) Microsoft Corporation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
using Microsoft.UI.Xaml.Controls;
6+
using System.Diagnostics;
7+
using Windows.UI.Xaml;
8+
using Windows.UI.Xaml.Controls;
9+
using Windows.UI.Xaml.Media;
10+
11+
namespace WebView2_UWP.Pages
12+
{
13+
public class BasePage : Page
14+
{
15+
public BasePage()
16+
{
17+
Unloaded += BasePage_Unloaded;
18+
}
19+
20+
private void BasePage_Unloaded(object sender, RoutedEventArgs e)
21+
{
22+
// The garbage collector can be slow to dispose of the
23+
// webviews. Manually free the resources being used by
24+
// the webviews since they are no longer needed after
25+
// the page has been unloaded.
26+
CloseAllWebViews(this);
27+
}
28+
29+
private void CloseAllWebViews(DependencyObject root)
30+
{
31+
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
32+
{
33+
var childVisual = VisualTreeHelper.GetChild(root, i);
34+
CloseAllWebViews(childVisual);
35+
36+
if (childVisual is WebView2 webView)
37+
{
38+
webView.Close();
39+
}
40+
}
41+
}
42+
}
43+
}

SampleApps/webview2_sample_uwp/Pages/Browser.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
Use of this source code is governed by a BSD-style license that can be
33
found in the LICENSE file.-->
44

5-
<Page
5+
<local:BasePage
66
x:Class="WebView2_UWP.Pages.Browser"
77
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
88
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
99
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
1010
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1111
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
12+
xmlns:local="using:WebView2_UWP.Pages"
1213
mc:Ignorable="d"
1314
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1415

@@ -45,4 +46,4 @@ found in the LICENSE file.-->
4546
<Rectangle Grid.Row="2" Fill="LightGray"/>
4647
<TextBlock x:Name="StatusBar" Text="WebView2" VerticalAlignment="Center" Grid.Row="2" Margin="10,0,10,0"/>
4748
</Grid>
48-
</Page>
49+
</local:BasePage>

SampleApps/webview2_sample_uwp/Pages/Browser.xaml.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
namespace WebView2_UWP.Pages
2121
{
22-
public sealed partial class Browser : Page
22+
public sealed partial class Browser : BasePage
2323
{
2424
private string _homeUrl = "https://developer.microsoft.com/en-us/microsoft-edge/webview2/";
2525

2626
public Browser()
2727
{
28-
this.InitializeComponent();
28+
InitializeComponent();
2929
AddressBar.Text = _homeUrl;
3030

3131
WebView2.CoreWebView2Initialized += WebView2_CoreWebView2Initialized;
@@ -36,7 +36,11 @@ public Browser()
3636
WebView2.Source = new Uri(AddressBar.Text);
3737
}
3838

39+
#if USE_WEBVIEW2_SMOKETEST
3940
private async void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
41+
#else
42+
private void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
43+
#endif
4044
{
4145
if (args.Exception != null)
4246
{
@@ -59,7 +63,7 @@ private async void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView
5963
}
6064

6165
// <DownloadStarting>
62-
private async void CoreWebView2_DownloadStarting(CoreWebView2 sender, CoreWebView2DownloadStartingEventArgs args)
66+
private void CoreWebView2_DownloadStarting(CoreWebView2 sender, CoreWebView2DownloadStartingEventArgs args)
6367
{
6468
// Developer can obtain a deferral for the event so that the CoreWebView2
6569
// doesn't examine the properties we set on the event args until
@@ -142,13 +146,17 @@ private void StatusUpdate(string message)
142146
Debug.WriteLine(message);
143147
}
144148

145-
private async void WebView2_NavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args)
149+
private void WebView2_NavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args)
146150
{
147151
RefreshButton.IsEnabled = false;
148152
CancelButton.IsEnabled = true;
149153
}
150154

155+
#if USE_WEBVIEW2_SMOKETEST
151156
private async void WebView2_NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
157+
#else
158+
private void WebView2_NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
159+
#endif
152160
{
153161
StatusUpdate("Navigation complete");
154162

SampleApps/webview2_sample_uwp/Pages/ExecuteJavascript.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
Use of this source code is governed by a BSD-style license that can be
33
found in the LICENSE file.-->
44

5-
<Page
5+
<local:BasePage
66
x:Class="WebView2_UWP.Pages.ExecuteJavascript"
77
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
88
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
9-
xmlns:local="using:WebView2_UWP"
109
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
1110
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1211
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
12+
xmlns:local="using:WebView2_UWP.Pages"
1313
mc:Ignorable="d"
1414
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1515

@@ -83,5 +83,5 @@ found in the LICENSE file.-->
8383

8484
<controls:WebView2 Grid.Column="1" x:Name="WebView2" />
8585
</Grid>
86-
</Page>
86+
</local:BasePage>
8787

SampleApps/webview2_sample_uwp/Pages/ExecuteJavascript.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace WebView2_UWP.Pages
1212
{
13-
public sealed partial class ExecuteJavascript : Page
13+
public sealed partial class ExecuteJavascript : BasePage
1414
{
1515
Dictionary<string, string> codeSnippets = new Dictionary<string, string>()
1616
{
@@ -29,7 +29,7 @@ public ExecuteJavascript()
2929
WebView2.Source = new Uri("http://appassets.html.example/execute_javascript.html");
3030
}
3131

32-
private async void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
32+
private void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
3333
{
3434
sender.CoreWebView2.SetVirtualHostNameToFolderMapping("appassets.html.example", "html", CoreWebView2HostResourceAccessKind.Allow);
3535
}

SampleApps/webview2_sample_uwp/Pages/NewWindow.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
Use of this source code is governed by a BSD-style license that can be
33
found in the LICENSE file.-->
44

5-
<Page
5+
<local:BasePage
66
x:Class="WebView2_UWP.Pages.NewWindow"
77
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
88
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
9-
xmlns:local="using:WebView2_UWP.Pages"
109
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
1110
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
1211
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
12+
xmlns:local="using:WebView2_UWP.Pages"
1313
mc:Ignorable="d"
1414
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1515

@@ -78,4 +78,4 @@ found in the LICENSE file.-->
7878
</Grid>
7979

8080
</Grid>
81-
</Page>
81+
</local:BasePage>

SampleApps/webview2_sample_uwp/Pages/NewWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace WebView2_UWP.Pages
1212
{
13-
public sealed partial class NewWindow : Page
13+
public sealed partial class NewWindow : BasePage
1414
{
1515
public enum OptionEnum
1616
{
@@ -65,7 +65,7 @@ public NewWindow()
6565
WebView2.Source = new Uri("http://appassets.html.example/new_window.html");
6666
}
6767

68-
private async void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
68+
private void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
6969
{
7070
sender.CoreWebView2.SetVirtualHostNameToFolderMapping("appassets.html.example", "html", CoreWebView2HostResourceAccessKind.Allow);
7171
sender.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;

0 commit comments

Comments
 (0)