Skip to content

Commit c743ee4

Browse files
authored
Merge pull request #260 from MicrosoftEdge/user/champnic/WV2CC_Sample
Add simple sample app for WV2CC
2 parents 3d18366 + 8532634 commit c743ee4

File tree

9 files changed

+259
-0
lines changed

9 files changed

+259
-0
lines changed

Diff for: SampleApps/WebView2WpfCompositionControl/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
description: "Demonstrates the features and usage patterns of WebView2CompositionControl in a WPF app."
3+
extendedZipContent:
4+
-
5+
path: SharedContent
6+
target: SharedContent
7+
-
8+
path: LICENSE
9+
target: LICENSE
10+
languages:
11+
- cpp
12+
page_type: sample
13+
products:
14+
- microsoft-edge
15+
---
16+
# WebView2WpfCompositionControl sample app
17+
18+
<!-- only enough info to differentiate this sample vs. the others; what is different about this sample compared to the sibling samples? -->
19+
20+
<!-- distinctive platform: -->
21+
This sample, **WebView2WpfCompositionControl**, embeds a WPF WebView2CompositionControl within a .NET Core 3.0 and/or .NET 8 WPF application. The WebView2CompositionControl solves the WPF "Airspace" issue (where all WPF content showed up below the WebView2 in the regular WPF control) by using D3D11 graphics capture session to show and interact with WebView2 content in a regular WPF WebView2 control. In general, the WebView2CompositionControl can be used as a stand-in replacement for the regular WPF WebView2 control.
22+
23+
<!-- distinctive project type and language: -->
24+
This sample is built as a WPF .NET Core application in Visual Studio 2022.
25+
26+
<!-- screenshot of running sample app: -->
27+
This is the main WebView2 sample. The running **WebView2APISample** app window shows the WebView2 SDK version and also the WebView2 Runtime version and path. The **WebView2APISample** app has several menus containing many menuitems that demonstrate a broad range of WebView2 APIs:
28+
29+
![The WebView2WpfCompositionControl sample app running](./documentation/screenshot.png)
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 17
4+
VisualStudioVersion = 17.11.35431.28
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebView2WpfCompositionControl", "WebView2WpfCompositionControl\WebView2WpfCompositionControl.csproj", "{B4C22133-B97C-4593-AB64-BE1694A0EC21}"
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+
{B4C22133-B97C-4593-AB64-BE1694A0EC21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B4C22133-B97C-4593-AB64-BE1694A0EC21}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B4C22133-B97C-4593-AB64-BE1694A0EC21}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B4C22133-B97C-4593-AB64-BE1694A0EC21}.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 = {0E35B434-5EE7-4941-835D-0E7BD2B4D824}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="WebView2WpfCompositionControl.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:WebView2WpfCompositionControl"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Configuration;
2+
using System.Data;
3+
using System.Windows;
4+
5+
namespace WebView2WpfCompositionControl
6+
{
7+
/// <summary>
8+
/// Interaction logic for App.xaml
9+
/// </summary>
10+
public partial class App : Application
11+
{
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<Window x:Class="WebView2WpfCompositionControl.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:WebView2WpfCompositionControl"
7+
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
8+
mc:Ignorable="d"
9+
Title="MainWindow" Height="450" Width="800">
10+
<DockPanel>
11+
12+
<Grid
13+
DockPanel.Dock="Top">
14+
<Grid.ColumnDefinitions>
15+
<ColumnDefinition Width="3*" />
16+
<ColumnDefinition Width="40" />
17+
<ColumnDefinition Width="120" />
18+
<ColumnDefinition Width="*" />
19+
</Grid.ColumnDefinitions>
20+
<TextBox
21+
x:Name="addressBar"
22+
Grid.Column="0"
23+
GotFocus="AddressBar_GotFocus"/>
24+
<Button
25+
x:Name="goButton"
26+
Grid.Column="1"
27+
Margin="5,0"
28+
Click="GoButton_Click"
29+
Content="Go"
30+
GotFocus="GoButton_GotFocus"/>
31+
<Button
32+
x:Name="toggleButton"
33+
Grid.Column="2"
34+
Margin="5,0"
35+
Click="ToggleButton_Click"
36+
Content="Toggle"
37+
GotFocus="ToggleButton_GotFocus"/>
38+
<Label
39+
x:Name="status"
40+
Grid.Column="3"
41+
Background="AliceBlue"
42+
Foreground="Gray"
43+
Content="Status"/>
44+
</Grid>
45+
<Grid>
46+
<wv2:WebView2CompositionControl
47+
x:Name="webView" />
48+
<Button
49+
x:Name="airspaceButton"
50+
Visibility="Collapsed"
51+
Content="This is a button over the WebView2!"
52+
Click="AirspaceButton_Click"
53+
Background="LightPink"
54+
Height="100"
55+
Width="300"
56+
GotFocus="AirspaceButton_GotFocus"/>
57+
</Grid>
58+
</DockPanel>
59+
60+
</Window>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System.Windows;
2+
using Microsoft.Web.WebView2.Core;
3+
4+
namespace WebView2WpfCompositionControl
5+
{
6+
/// <summary>
7+
/// Interaction logic for MainWindow.xaml
8+
/// </summary>
9+
public partial class MainWindow : Window
10+
{
11+
public MainWindow()
12+
{
13+
InitializeComponent();
14+
15+
webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
16+
webView.NavigationStarting += WebView_NavigationStarting;
17+
webView.NavigationCompleted += WebView_NavigationCompleted;
18+
webView.WebMessageReceived += WebView_WebMessageReceived;
19+
webView.Source = new Uri("https://www.bing.com");
20+
}
21+
22+
private void WebView_CoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e)
23+
{
24+
status.Content = "Initialized:" + (e.InitializationException?.ToString() ?? "Success");
25+
}
26+
27+
private void WebView_WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
28+
{
29+
status.Content = e.TryGetWebMessageAsString();
30+
}
31+
32+
private void WebView_NavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e)
33+
{
34+
status.Content = "NavigationStarting";
35+
}
36+
37+
private void WebView_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs args)
38+
{
39+
// Update address bar
40+
addressBar.Text = webView.Source.ToString();
41+
string info = (args.IsSuccess ? webView.CoreWebView2.DocumentTitle : "error");
42+
status.Content = $"NavigationCompleted:{info}";
43+
}
44+
45+
private void GoButton_Click(object sender, RoutedEventArgs e)
46+
{
47+
if (webView != null && webView.CoreWebView2 != null)
48+
{
49+
String uri = addressBar.Text;
50+
if (uri.StartsWith("http://")) { uri.Replace("http://", "https://"); }
51+
try
52+
{
53+
webView.CoreWebView2.Navigate(uri);
54+
}
55+
catch
56+
{
57+
status.Content = "InvalidUrl";
58+
}
59+
}
60+
}
61+
62+
private void ToggleButton_Click(object sender, RoutedEventArgs e)
63+
{
64+
airspaceButton.Visibility = (airspaceButton.IsVisible ? Visibility.Collapsed : Visibility.Visible);
65+
status.Content = "Toggle:" + airspaceButton.Visibility;
66+
}
67+
68+
private void AirspaceButton_Click(object sender, RoutedEventArgs e)
69+
{
70+
status.Content = "AirspaceClicked";
71+
}
72+
73+
private void AddressBar_GotFocus(object sender, RoutedEventArgs e)
74+
{
75+
status.Content = "GotFocus:AddressBar";
76+
}
77+
78+
private void GoButton_GotFocus(object sender, RoutedEventArgs e)
79+
{
80+
status.Content = "GotFocus:GoButton";
81+
}
82+
83+
private void ToggleButton_GotFocus(object sender, RoutedEventArgs e)
84+
{
85+
status.Content = "GotFocus:ToggleButton";
86+
}
87+
88+
private void AirspaceButton_GotFocus(object sender, RoutedEventArgs e)
89+
{
90+
status.Content = "GotFocus:AirspaceButton";
91+
}
92+
}
93+
94+
}
95+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFrameworks>net8.0-windows10.0.22000.0;netcoreapp3.0</TargetFrameworks>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<UseWPF>true</UseWPF>
9+
<LangVersion>10</LangVersion>
10+
<Configurations>Debug</Configurations>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.*-*" />
15+
</ItemGroup>
16+
17+
</Project>
799 KB
Loading

0 commit comments

Comments
 (0)