Skip to content

Commit 898dee8

Browse files
oggy22Ognjen Sobajicpeiche-msmonica-ch
authored
Update projects to use latest WebView2 SDK 1.0.824-prerelease (#84)
* Win32 builds and runs against 1.0.705.50-prerelease WPF and WinForms build and run against 1.0.705.50 (release version) * Win7 fix by removing api-ms-win-core-path-l1-1-0.dll dependency * Win32, WPF and WinForms build and run against (custom) 1.0.779-prerelease off user/champnic/DefaultBackgroundColorFix * Back to 721 * Update all projects to 1.0.781-prerelease. Fix PlatformTarget in WPF and WinForms. * Move projects to use WebView2 SDK 1.0.774.41-prerelease * Update projects to use the latest WebView2 SDK 1.0.824-prerelease * Fix some problems * Add CDP Sample app to WebView2Samples (#83) Co-authored-by: Ognjen Sobajic <[email protected]> Co-authored-by: Jessica Chen <[email protected]> Co-authored-by: monica-ch <[email protected]>
1 parent 72b60d0 commit 898dee8

29 files changed

+726
-156
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application x:Class="WV2CDPExtensionSample.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
StartupUri="MainWindow.xaml">
5+
<Application.Resources>
6+
7+
</Application.Resources>
8+
</Application>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Windows;
2+
3+
namespace WV2CDPExtensionSample
4+
{
5+
/// <summary>
6+
/// Interaction logic for App.xaml
7+
/// </summary>
8+
public partial class App : Application
9+
{
10+
public App()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<Window x:Class="WV2CDPExtensionSample.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:local="clr-namespace:WV2CDPExtensionSample"
8+
mc:Ignorable="d"
9+
Title="MainWindow" Height="450" Width="800">
10+
11+
<Window.CommandBindings>
12+
<CommandBinding Command="NavigationCommands.GoToPage" Executed="GoToPageCmdExecuted" CanExecute="GoToPageCmdCanExecute"/>
13+
</Window.CommandBindings>
14+
15+
<DockPanel>
16+
<Menu DockPanel.Dock="Top">
17+
<MenuItem Header="DevTools Commands">
18+
<MenuItem Header="FPS Counter">
19+
<MenuItem Header="Show" Click="ShowFPSCounter"/>
20+
<MenuItem Header="Hide" Click="HideFPSCounter"/>
21+
</MenuItem>
22+
<MenuItem Header="Page Scale">
23+
<MenuItem Header="Set to 4" Click="SetPageScaleTo4"/>
24+
<MenuItem Header="Reset" Click="ResetPageScale"/>
25+
</MenuItem>
26+
<MenuItem Header="Reload Page" Click="ReloadPage"/>
27+
<MenuItem Header="Output To Trace">
28+
<MenuItem Header="Snapshot of Page (as string) " Click="CaptureSnapshot"/>
29+
</MenuItem>
30+
<MenuItem Header="Cookie Management">
31+
<MenuItem Header="Get Cookies" Click="GetAllCookies"/>
32+
<MenuItem Header="Clear All Cookies" Click="ClearAllCookies"/>
33+
<MenuItem Header="Add Or Update Cookie" Click="AddOrUpdateCookie"/>
34+
</MenuItem>
35+
<MenuItem Header="Geo Location">
36+
<MenuItem Header="Set Geo Location" Click="SetGeolocation"/>
37+
<MenuItem Header="Clear Geo Location" Click="ClearGeolocation"/>
38+
</MenuItem>
39+
</MenuItem>
40+
<MenuItem Header="DevTools Events">
41+
<MenuItem Header="DataReceived">
42+
<MenuItem Header="Subscribe" Click="SubscribeToDataReceived"/>
43+
<MenuItem Header="Unsubscribe" Click="UnsubscribeFromDataReceived"/>
44+
</MenuItem>
45+
<MenuItem Header="AnimationCreated">
46+
<MenuItem Header="Subscribe" Click="SubscribeToAnimationCreated"/>
47+
<MenuItem Header="Unsubscribe" Click="UnsubscribeFromAnimationCreated"/>
48+
</MenuItem>
49+
<MenuItem Header="DocumentUpdated">
50+
<MenuItem Header="Subscribe" Click="SubscribeToDocumentUpdated"/>
51+
<MenuItem Header="Unsubscribe" Click="UnsubscribeFromDocumentUpdated"/>
52+
</MenuItem>
53+
<MenuItem Header="DownloadWillBegin">
54+
<MenuItem Header="Subscribe" Click="SubscribeToDownloadWillBegin"/>
55+
<MenuItem Header="Unsubscribe" Click="UnsubscribeFromDownloadWillBegin"/>
56+
</MenuItem>
57+
</MenuItem>
58+
</Menu>
59+
<DockPanel DockPanel.Dock="Top">
60+
<Button DockPanel.Dock="Right" Command="NavigationCommands.GoToPage" CommandParameter="{Binding ElementName=url,Path=Text}">Go</Button>
61+
<TextBox x:Name="url" Text="{Binding ElementName=webView,Path=Source,Mode=OneWay}">
62+
<TextBox.InputBindings>
63+
<KeyBinding Key="Return" Command="NavigationCommands.GoToPage" CommandParameter="{Binding ElementName=url,Path=Text}" />
64+
</TextBox.InputBindings>
65+
</TextBox>
66+
</DockPanel>
67+
<wv2:WebView2
68+
x:Name="webView"
69+
Source="https://www.bing.com/"
70+
/>
71+
</DockPanel>
72+
</Window>
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Windows;
4+
using System.Windows.Input;
5+
using Microsoft.Web.WebView2.Core;
6+
using Microsoft.Web.WebView2.Wpf;
7+
using Microsoft.Web.WebView2.Core.DevToolsProtocolExtension;
8+
using System.Text;
9+
using System.Linq;
10+
11+
namespace WV2CDPExtensionSample
12+
{
13+
/// <summary>
14+
/// Interaction logic for MainWindow.xaml
15+
/// </summary>
16+
public partial class MainWindow : Window
17+
{
18+
bool _isNavigating = false;
19+
public static RoutedCommand CallCDPMethodCommand = new RoutedCommand();
20+
DevToolsProtocolHelper _cdpHelper;
21+
DevToolsProtocolHelper cdpHelper
22+
{
23+
get
24+
{
25+
if (webView == null || webView.CoreWebView2 == null)
26+
{
27+
throw new Exception("Initialize WebView before using DevToolsProtocolHelper.");
28+
}
29+
30+
if (_cdpHelper == null)
31+
{
32+
_cdpHelper = webView.CoreWebView2.GetDevToolsProtocolHelper();
33+
}
34+
35+
return _cdpHelper;
36+
}
37+
}
38+
public MainWindow()
39+
{
40+
InitializeComponent();
41+
}
42+
43+
void GoToPageCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
44+
{
45+
e.CanExecute = webView != null && !_isNavigating;
46+
}
47+
48+
async void GoToPageCmdExecuted(object target, ExecutedRoutedEventArgs e)
49+
{
50+
await webView.EnsureCoreWebView2Async();
51+
webView.CoreWebView2.Navigate((string)e.Parameter);
52+
}
53+
54+
55+
#region CDP_COMMANDS
56+
async void ShowFPSCounter(object sender, RoutedEventArgs e)
57+
{
58+
await cdpHelper.Overlay.SetShowFPSCounterAsync(true);
59+
}
60+
61+
async void HideFPSCounter(object sender, RoutedEventArgs e)
62+
{
63+
await cdpHelper.Overlay.SetShowFPSCounterAsync(false);
64+
}
65+
66+
async void SetPageScaleTo4(object sender, RoutedEventArgs e)
67+
{
68+
await cdpHelper.Emulation.SetPageScaleFactorAsync(4);
69+
}
70+
71+
async void ResetPageScale(object sender, RoutedEventArgs e)
72+
{
73+
await cdpHelper.Emulation.SetPageScaleFactorAsync(1);
74+
}
75+
76+
async void ReloadPage(object sender, RoutedEventArgs e)
77+
{
78+
await cdpHelper.Page.ReloadAsync();
79+
}
80+
81+
async void CaptureSnapshot(object sender, RoutedEventArgs e)
82+
{
83+
Trace.WriteLine(await cdpHelper.Page.CaptureSnapshotAsync());
84+
}
85+
86+
async void GetAllCookies(object sender, RoutedEventArgs e)
87+
{
88+
Network.Cookie[] cookies = await cdpHelper.Network.GetAllCookiesAsync();
89+
StringBuilder cookieResult = new StringBuilder(cookies.Count() + " cookie(s) received\n");
90+
foreach (var cookie in cookies)
91+
{
92+
cookieResult.Append($"\n{cookie.Name} {cookie.Value} {(cookie.Session ? "[session cookie]" : cookie.Expires.ToString("G"))}");
93+
}
94+
MessageBox.Show(cookieResult.ToString(), "Cookies");
95+
}
96+
97+
async void AddOrUpdateCookie(object target, RoutedEventArgs e)
98+
{
99+
bool cookie = await cdpHelper.Network.SetCookieAsync("CookieName", "CookieValue", null, "https://www.bing.com/");
100+
MessageBox.Show(cookie ? "Cookie is added/updated successfully" : "Error adding/updating cookie", "Cookies");
101+
}
102+
103+
async void ClearAllCookies(object sender, RoutedEventArgs e)
104+
{
105+
await cdpHelper.Network.ClearBrowserCookiesAsync();
106+
MessageBox.Show("Browser cookies are deleted", "Cookies");
107+
}
108+
109+
async void SetGeolocation(object sender, RoutedEventArgs e)
110+
{
111+
double latitude = 36.553085;
112+
double longitude = 103.97543;
113+
double accuracy = 1;
114+
await cdpHelper.Emulation.SetGeolocationOverrideAsync(latitude, longitude, accuracy);
115+
MessageBox.Show("Overridden the Geolocation Position", "Geolocation");
116+
}
117+
118+
async void ClearGeolocation(object sender, RoutedEventArgs e)
119+
{
120+
await cdpHelper.Emulation.ClearGeolocationOverrideAsync();
121+
MessageBox.Show("Cleared overridden Geolocation Position", "Geolocation");
122+
}
123+
#endregion
124+
125+
#region CDP_EVENTS
126+
async void SubscribeToDataReceived(object sender, RoutedEventArgs e)
127+
{
128+
await cdpHelper.Network.EnableAsync();
129+
cdpHelper.Network.DataReceived += PrintDataReceived;
130+
MessageBox.Show("Subscribed to DataReceived Event!", "DataReceived");
131+
}
132+
133+
void UnsubscribeFromDataReceived(object sender, RoutedEventArgs e)
134+
{
135+
cdpHelper.Network.DataReceived -= PrintDataReceived;
136+
MessageBox.Show("Unsubscribed from DataReceived Event!", "DataReceived");
137+
}
138+
139+
void PrintDataReceived(object sender, Network.DataReceivedEventArgs args)
140+
{
141+
Trace.WriteLine(String.Format("DataReceived Event Args - Timestamp: {0} Request Id: {1} DataLength: {2}", args.Timestamp, args.RequestId, args.DataLength));
142+
}
143+
144+
async void SubscribeToAnimationCreated(object sender, RoutedEventArgs e)
145+
{
146+
await cdpHelper.Animation.EnableAsync();
147+
cdpHelper.Animation.AnimationCreated += PrintAnimationCreated;
148+
MessageBox.Show("Subscribed to AnimationCreated Event!", "AnimationCreated");
149+
}
150+
151+
void UnsubscribeFromAnimationCreated(object sender, RoutedEventArgs e)
152+
{
153+
cdpHelper.Animation.AnimationCreated -= PrintAnimationCreated;
154+
MessageBox.Show("Unsubscribed from AnimationCreated Event!", "AnimationCreated");
155+
}
156+
157+
void PrintAnimationCreated(object sender, Animation.AnimationCreatedEventArgs args)
158+
{
159+
Trace.WriteLine(String.Format("AnimationCreated Event Args - Id: {0}", args.Id));
160+
}
161+
162+
async void SubscribeToDocumentUpdated(object sender, RoutedEventArgs e)
163+
{
164+
await cdpHelper.DOM.EnableAsync();
165+
cdpHelper.DOM.DocumentUpdated += PrintDocumentUpdated;
166+
MessageBox.Show("Subscribed to DocumentUpdated Event!", "DocumentUpdated");
167+
}
168+
169+
void UnsubscribeFromDocumentUpdated(object sender, RoutedEventArgs e)
170+
{
171+
cdpHelper.DOM.DocumentUpdated -= PrintDocumentUpdated;
172+
MessageBox.Show("Unsubscribed from DocumentUpdated Event!", "DocumentUpdated");
173+
}
174+
175+
void PrintDocumentUpdated(object sender, DOM.DocumentUpdatedEventArgs args)
176+
{
177+
Trace.WriteLine("DocumentUpdated Event Args - No Parameters", "DocumentUpdated");
178+
}
179+
180+
async void SubscribeToDownloadWillBegin(object sender, RoutedEventArgs e)
181+
{
182+
await cdpHelper.Page.EnableAsync();
183+
cdpHelper.Page.DownloadWillBegin += PrintDownloadWillBegin;
184+
MessageBox.Show("Subscribed to DownloadWillBegin Event!", "DownloadWillBegin");
185+
}
186+
187+
void UnsubscribeFromDownloadWillBegin(object sender, RoutedEventArgs e)
188+
{
189+
cdpHelper.Page.DownloadWillBegin -= PrintDownloadWillBegin;
190+
MessageBox.Show("Unsubscribed from DownloadWillBegin Event!", "DownloadWillBegin");
191+
}
192+
193+
void PrintDownloadWillBegin(object sender, Page.DownloadWillBeginEventArgs args)
194+
{
195+
Trace.WriteLine(String.Format("DownloadWillBegin Event Args - FrameId: {0} Guid: {1} URL: {2}", args.FrameId, args.Guid, args.Url));
196+
}
197+
#endregion
198+
}
199+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
description: "Demonstrate the usage patterns of WebView2 CDP Extension in WPF."
3+
extendedZipContent:
4+
-
5+
path: SharedContent
6+
target: SharedContent
7+
-
8+
path: LICENSE
9+
target: LICENSE
10+
languages:
11+
- csharp
12+
page_type: sample
13+
products:
14+
- microsoft-edge
15+
urlFragment: WV2CDPExtensionSample
16+
---
17+
# WebView2 CDP Extension
18+
19+
This application built with the [WebView2 CDP Extension](https://aka.ms/webviewcdp) that defines all CDP methods, events, and types.
20+
21+
The WV2CDPExtensionSample showcases of Utilizing Chrome DevTools Protocol functions using a DevToolsProtocolHelper object in WebView2. It is built as a WPF [Visual Studio 2019](https://visualstudio.microsoft.com/vs/) project and makes use of C# in the WebView2 environment.
22+
23+
If this is your first time using WebView2, we recommend first following the [Getting Started](https://docs.microsoft.com/microsoft-edge/webview2/gettingstarted/wpf) guide, which goes over how to create a WebView2 and walks through some basic WebView2 functionality.
24+
25+
## Prerequisites
26+
27+
- [Microsoft Edge (Chromium)](https://www.microsoftedgeinsider.com/download/) installed on a supported OS. Currently we recommend the latest version of the Edge Canary channel.
28+
- [Visual Studio](https://visualstudio.microsoft.com/vs/) with .NET support installed.
29+
- Latest version of our [WebView2 SDK](https://aka.ms/webviewnuget), which is included in this project.
30+
- Latest release version of our [WebView2 CDP Extension](https://aka.ms/webviewcdpnuget), which is included in this project.
31+
32+
## Build the WebView2 WPF Browser
33+
34+
Clone the repository and open the solution in Visual Studio. WebView2 and WebView2 DevToolsProtocolExtension is already included as a NuGet package* in this project.
35+
36+
- Open the solution in Visual Studio 2019**
37+
- Set the target you want to build (Debug/Release, AnyCPU)
38+
- Build the project file: _WV2CDPExtensionSample.csproj_
39+
40+
That's it! Everything should be ready to just launch the app.
41+
42+
*You can get the WebView2 and WebView2 DevToolsProtocolExtension NugetPackage through the Visual Studio NuGet Package Manager.
43+
44+
**You can also use Visual Studio 2017 by changing the project's Platform Toolset in Project Properties/Configuration properties/General/Platform Toolset. You might also need to change the Windows SDK to the latest version available to you.
45+
46+
## Code of Conduct
47+
48+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [email protected] with any additional questions or comments.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
2+
<PropertyGroup>
3+
<Platforms>AnyCPU</Platforms>
4+
<OutputType>WinExe</OutputType>
5+
<AssemblyName>WV2CDPExtensionWPFSample</AssemblyName>
6+
<TargetFrameworks>netcoreapp3.0;net462</TargetFrameworks>
7+
<Product>WebView2 CDP Extension WPF Sample App</Product>
8+
<Copyright>Copyright ©2020</Copyright>
9+
<UseWPF>true</UseWPF>
10+
<Authors>Hybrid Applications Team</Authors>
11+
<Company>Microsoft</Company>
12+
</PropertyGroup>
13+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
14+
<DebugType>full</DebugType>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
17+
<DebugType>pdbonly</DebugType>
18+
</PropertyGroup>
19+
<ItemGroup>
20+
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.774.44" />
21+
<PackageReference Include="Microsoft.Web.WebView2.DevToolsProtocolExtension" Version="1.0.824" />
22+
<PackageReference Include="System.Text.Json" Version="4.7.2" />
23+
</ItemGroup>
24+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31005.135
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WV2CDPExtensionWPFSample", "WV2CDPExtensionWPFSample.csproj", "{86E127C0-CDC3-495F-871A-E7A2CF6F6D4D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{86E127C0-CDC3-495F-871A-E7A2CF6F6D4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{86E127C0-CDC3-495F-871A-E7A2CF6F6D4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{86E127C0-CDC3-495F-871A-E7A2CF6F6D4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{86E127C0-CDC3-495F-871A-E7A2CF6F6D4D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {5F64ADA8-609E-41F6-9CFB-30E57EEF1E63}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)