Skip to content

Commit 30faed2

Browse files
authored
Merge pull request #176 from MicrosoftEdge/user/pieths-ms/add_settings_page_and_version_info_to_the_uwp_sample_app
Add script debugging, settings and version to the UWP sample app.
2 parents dd78334 + 1231b0a commit 30faed2

20 files changed

+905
-24
lines changed

SampleApps/webview2_sample_uwp/App.xaml.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
using System;
66
using WebView2_UWP;
7+
using WebView2_UWP.Pages;
78
using Windows.ApplicationModel;
89
using Windows.ApplicationModel.Activation;
10+
using Windows.UI.ViewManagement;
911
using Windows.UI.Xaml;
1012
using Windows.UI.Xaml.Controls;
1113
using Windows.UI.Xaml.Navigation;
@@ -17,14 +19,30 @@ namespace webview2_sample_uwp
1719
/// </summary>
1820
sealed partial class App : Application
1921
{
22+
private readonly Settings _settings;
23+
24+
public Settings Settings
25+
{
26+
get { return _settings; }
27+
}
28+
29+
// Syntactic sugar to avoid having to cast
30+
// to App in other parts of the application.
31+
public static App Instance
32+
{
33+
get { return (App)Current; }
34+
}
35+
2036
/// <summary>
2137
/// Initializes the singleton application object. This is the first line of authored code
2238
/// executed, and as such is the logical equivalent of main() or WinMain().
2339
/// </summary>
2440
public App()
2541
{
26-
this.InitializeComponent();
27-
this.Suspending += OnSuspending;
42+
_settings = new Settings();
43+
44+
InitializeComponent();
45+
Suspending += OnSuspending;
2846
}
2947

3048
/// <summary>
@@ -91,5 +109,17 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
91109
//TODO: Save application state and stop any background activity
92110
deferral.Complete();
93111
}
112+
113+
public void UpdateAppTitle(string webViewVersion)
114+
{
115+
var title = "";
116+
117+
if (_settings.ShowWebViewVersionInTitleBar)
118+
{
119+
title = "WebView2 Version:" + webViewVersion;
120+
}
121+
122+
ApplicationView.GetForCurrentView().Title = title;
123+
}
94124
}
95125
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<!--Copyright (C) Microsoft Corporation. All rights reserved.
3+
Use of this source code is governed by a BSD-style license that can be
4+
found in the LICENSE file.-->
5+
6+
<html>
7+
8+
<head>
9+
<meta charset="UTF-8">
10+
<title>WebView Script Debugging Example</title>
11+
</head>
12+
13+
<body>
14+
<script>
15+
console.log("This is the very first line of code that executes.");
16+
console.log("Second");
17+
</script>
18+
19+
20+
<script>
21+
console.log("Third");
22+
</script>
23+
24+
<div class="container">
25+
<div class="banner container">
26+
<h1 id="header">WebView Example with JavaScript Debugging</h1>
27+
</div>
28+
<button id="addButton">Add a new item</button>
29+
<div class="container" id="list"></div>
30+
</div>
31+
32+
<script src="ScenarioJavaScriptDebugIndex.js"></script>
33+
34+
<script>
35+
console.log("End");
36+
</script>
37+
38+
</body>
39+
40+
</html>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function onAddClick() {
2+
console.log("onAddClick+");
3+
const list = document.getElementById('list');
4+
const index = (list.childElementCount + 1);
5+
const x = 0;
6+
const y = 0;
7+
8+
const item = document.createElement('div');
9+
item.classList.add('item');
10+
item.classList.add('container');
11+
const title = document.createElement('div');
12+
title.classList.add('title');
13+
title.innerText = 'Item ' + index;
14+
item.appendChild(title);
15+
const content = document.createElement('div');
16+
content.classList.add('content');
17+
18+
if (index % 2 === 0) {
19+
content.style.backgroundImage =
20+
`url(https://edgetipscdn.microsoft.com/insider-site/images/logo-dev.c8d75c3b.png)`;
21+
} else {
22+
content.style.backgroundImage =
23+
`url(https://edgetipscdn.microsoft.com/insider-site/images/logo-canary.a897af1f.png)`;
24+
}
25+
26+
item.appendChild(content);
27+
list.appendChild(item);
28+
console.log("onAddClick-");
29+
}
30+
31+
console.log("inside");
32+
// Add the event handler as soon as the script loads
33+
const elem = document.getElementById('addButton');
34+
elem.addEventListener('click', onAddClick);
35+
36+
// Fill in some items for testing
37+
for (let i = 0; i < 2; i++) {
38+
onAddClick();
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<!--Copyright (C) Microsoft Corporation. All rights reserved.
3+
Use of this source code is governed by a BSD-style license that can be
4+
found in the LICENSE file.-->
5+
6+
<html>
7+
8+
<head>
9+
<meta charset="UTF-8">
10+
<title>WebView Script Debugging Example</title>
11+
</head>
12+
13+
<body>
14+
<script>
15+
console.log("This is the very first line of code that executes.");
16+
console.log("Second");
17+
</script>
18+
19+
20+
<script>
21+
console.log("Third");
22+
</script>
23+
24+
<div class="container">
25+
<div class="banner container">
26+
<h1 id="header">WebView Example with TypeScript Debugging</h1>
27+
</div>
28+
<button id="getHeader">Get the Current Page Header</button>
29+
<div id="HeaderSpace"></div>
30+
</div>
31+
32+
<script src="ScenarioTypeScriptDebugIndex.ts"></script>
33+
34+
<script>
35+
console.log("End");
36+
</script>
37+
38+
</body>
39+
40+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function onHeaderClick() {
2+
console.log("onHeaderClick+");
3+
const header = document.getElementById('header');
4+
document.getElementById('HeaderSpace').innerHTML = header.innerHTML;
5+
console.log("onHeaderClick-");
6+
}
7+
8+
console.log("inside");
9+
// Add the event handler as soon as the script loads
10+
const elem = document.getElementById('getHeader');
11+
elem.addEventListener('click', onHeaderClick);

SampleApps/webview2_sample_uwp/MSOWNERS

Lines changed: 0 additions & 1 deletion
This file was deleted.

SampleApps/webview2_sample_uwp/Pages/Browser.xaml

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

55
<Page
6-
x:Class="webview2_sample_uwp.Browser"
6+
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"
9-
xmlns:local="using:webview2_sample_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"

SampleApps/webview2_sample_uwp/Pages/Browser.xaml.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
using System;
1010
using System.Diagnostics;
1111
using System.IO;
12+
using webview2_sample_uwp;
1213
#if USE_WEBVIEW2_SMOKETEST
1314
using Windows.Storage;
1415
#endif
1516
using Windows.UI.Xaml;
1617
using Windows.UI.Xaml.Controls;
1718
using Windows.UI.Xaml.Input;
1819

19-
namespace webview2_sample_uwp
20+
namespace WebView2_UWP.Pages
2021
{
2122
public sealed partial class Browser : Page
2223
{
@@ -27,30 +28,34 @@ public Browser()
2728
this.InitializeComponent();
2829
AddressBar.Text = _homeUrl;
2930

30-
#if USE_WEBVIEW2_SMOKETEST
31-
Environment.SetEnvironmentVariable("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", ApplicationData.Current.LocalFolder.Path + "\\EdgeBin");
32-
#endif
3331
WebView2.CoreWebView2Initialized += WebView2_CoreWebView2Initialized;
3432
WebView2.NavigationStarting += WebView2_NavigationStarting;
3533
WebView2.NavigationCompleted += WebView2_NavigationCompleted;
3634

37-
WebView2.Source = new Uri(AddressBar.Text);
3835
StatusUpdate("Ready");
36+
WebView2.Source = new Uri(AddressBar.Text);
3937
}
4038

4139
private async void WebView2_CoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs args)
4240
{
43-
#if USE_WEBVIEW2_SMOKETEST
4441
if (args.Exception != null)
4542
{
43+
StatusUpdate($"Error initializing WebView2: {args.Exception.Message}");
44+
45+
#if USE_WEBVIEW2_SMOKETEST
4646
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
4747
StorageFile file = await localFolder.CreateFileAsync(@"Failure.txt", CreationCollisionOption.FailIfExists);
4848
await FileIO.WriteTextAsync(file, args.Exception.ToString());
4949
Environment.Exit(1);
50-
}
5150
#endif
52-
WebView2.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
53-
WebView2.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;
51+
}
52+
else
53+
{
54+
App.Instance.UpdateAppTitle(sender.GetExtendedVersionString(false));
55+
56+
WebView2.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
57+
WebView2.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged;
58+
}
5459
}
5560

5661
// <DownloadStarting>

SampleApps/webview2_sample_uwp/Pages/ExecuteJavascript.xaml

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

55
<Page
6-
x:Class="WebView2_UWP.ExecuteJavascript"
6+
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"
99
xmlns:local="using:WebView2_UWP"

SampleApps/webview2_sample_uwp/Pages/ExecuteJavascript.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Collections.Generic;
99
using Windows.UI.Xaml.Controls;
1010

11-
namespace WebView2_UWP
11+
namespace WebView2_UWP.Pages
1212
{
1313
public sealed partial class ExecuteJavascript : Page
1414
{

0 commit comments

Comments
 (0)