-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
description: "Demonstrates the features and usage patterns of WebView2CompositionControl in a WPF app." | ||
extendedZipContent: | ||
- | ||
path: SharedContent | ||
target: SharedContent | ||
- | ||
path: LICENSE | ||
target: LICENSE | ||
languages: | ||
- cpp | ||
page_type: sample | ||
products: | ||
- microsoft-edge | ||
--- | ||
# WebView2WpfCompositionControl sample app | ||
|
||
<!-- only enough info to differentiate this sample vs. the others; what is different about this sample compared to the sibling samples? --> | ||
|
||
<!-- distinctive platform: --> | ||
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. | ||
|
||
<!-- distinctive project type and language: --> | ||
This sample is built as a WPF .NET Core application in Visual Studio 2022. | ||
|
||
<!-- screenshot of running sample app: --> | ||
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: | ||
|
||
![The WebView2WpfCompositionControl sample app running](./documentation/screenshot.png) |
25 changes: 25 additions & 0 deletions
25
SampleApps/WebView2WpfCompositionControl/WebView2WpfCompositionControl.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.11.35431.28 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebView2WpfCompositionControl", "WebView2WpfCompositionControl\WebView2WpfCompositionControl.csproj", "{B4C22133-B97C-4593-AB64-BE1694A0EC21}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B4C22133-B97C-4593-AB64-BE1694A0EC21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B4C22133-B97C-4593-AB64-BE1694A0EC21}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B4C22133-B97C-4593-AB64-BE1694A0EC21}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B4C22133-B97C-4593-AB64-BE1694A0EC21}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {0E35B434-5EE7-4941-835D-0E7BD2B4D824} | ||
EndGlobalSection | ||
EndGlobal |
9 changes: 9 additions & 0 deletions
9
SampleApps/WebView2WpfCompositionControl/WebView2WpfCompositionControl/App.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Application x:Class="WebView2WpfCompositionControl.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:WebView2WpfCompositionControl" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
14 changes: 14 additions & 0 deletions
14
SampleApps/WebView2WpfCompositionControl/WebView2WpfCompositionControl/App.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Windows; | ||
|
||
namespace WebView2WpfCompositionControl | ||
{ | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
SampleApps/WebView2WpfCompositionControl/WebView2WpfCompositionControl/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Windows; | ||
|
||
[assembly: ThemeInfo( | ||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located | ||
//(used if a resource is not found in the page, | ||
// or application resource dictionaries) | ||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located | ||
//(used if a resource is not found in the page, | ||
// app, or any theme specific resource dictionaries) | ||
)] |
60 changes: 60 additions & 0 deletions
60
SampleApps/WebView2WpfCompositionControl/WebView2WpfCompositionControl/MainWindow.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<Window x:Class="WebView2WpfCompositionControl.MainWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:WebView2WpfCompositionControl" | ||
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf" | ||
mc:Ignorable="d" | ||
Title="MainWindow" Height="450" Width="800"> | ||
<DockPanel> | ||
|
||
<Grid | ||
DockPanel.Dock="Top"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="3*" /> | ||
<ColumnDefinition Width="40" /> | ||
<ColumnDefinition Width="120" /> | ||
<ColumnDefinition Width="*" /> | ||
</Grid.ColumnDefinitions> | ||
<TextBox | ||
x:Name="addressBar" | ||
Grid.Column="0" | ||
GotFocus="AddressBar_GotFocus"/> | ||
<Button | ||
x:Name="goButton" | ||
Grid.Column="1" | ||
Margin="5,0" | ||
Click="GoButton_Click" | ||
Content="Go" | ||
GotFocus="GoButton_GotFocus"/> | ||
<Button | ||
x:Name="toggleButton" | ||
Grid.Column="2" | ||
Margin="5,0" | ||
Click="ToggleButton_Click" | ||
Content="Toggle" | ||
GotFocus="ToggleButton_GotFocus"/> | ||
<Label | ||
x:Name="status" | ||
Grid.Column="3" | ||
Background="AliceBlue" | ||
Foreground="Gray" | ||
Content="Status"/> | ||
</Grid> | ||
<Grid> | ||
<wv2:WebView2CompositionControl | ||
x:Name="webView" /> | ||
<Button | ||
x:Name="airspaceButton" | ||
Visibility="Collapsed" | ||
Content="This is a button over the WebView2!" | ||
Click="AirspaceButton_Click" | ||
Background="LightPink" | ||
Height="100" | ||
Width="300" | ||
GotFocus="AirspaceButton_GotFocus"/> | ||
</Grid> | ||
</DockPanel> | ||
|
||
</Window> |
95 changes: 95 additions & 0 deletions
95
SampleApps/WebView2WpfCompositionControl/WebView2WpfCompositionControl/MainWindow.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Windows; | ||
using Microsoft.Web.WebView2.Core; | ||
|
||
namespace WebView2WpfCompositionControl | ||
{ | ||
/// <summary> | ||
/// Interaction logic for MainWindow.xaml | ||
/// </summary> | ||
public partial class MainWindow : Window | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
|
||
webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted; | ||
webView.NavigationStarting += WebView_NavigationStarting; | ||
webView.NavigationCompleted += WebView_NavigationCompleted; | ||
webView.WebMessageReceived += WebView_WebMessageReceived; | ||
webView.Source = new Uri("https://www.bing.com"); | ||
} | ||
|
||
private void WebView_CoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e) | ||
{ | ||
status.Content = "Initialized:" + (e.InitializationException?.ToString() ?? "Success"); | ||
} | ||
|
||
private void WebView_WebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e) | ||
{ | ||
status.Content = e.TryGetWebMessageAsString(); | ||
} | ||
|
||
private void WebView_NavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs e) | ||
{ | ||
status.Content = "NavigationStarting"; | ||
} | ||
|
||
private void WebView_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs args) | ||
{ | ||
// Update address bar | ||
addressBar.Text = webView.Source.ToString(); | ||
string info = (args.IsSuccess ? webView.CoreWebView2.DocumentTitle : "error"); | ||
status.Content = $"NavigationCompleted:{info}"; | ||
} | ||
|
||
private void GoButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
if (webView != null && webView.CoreWebView2 != null) | ||
{ | ||
String uri = addressBar.Text; | ||
if (uri.StartsWith("http://")) { uri.Replace("http://", "https://"); } | ||
try | ||
{ | ||
webView.CoreWebView2.Navigate(uri); | ||
} | ||
catch | ||
{ | ||
status.Content = "InvalidUrl"; | ||
} | ||
} | ||
} | ||
|
||
private void ToggleButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
airspaceButton.Visibility = (airspaceButton.IsVisible ? Visibility.Collapsed : Visibility.Visible); | ||
status.Content = "Toggle:" + airspaceButton.Visibility; | ||
} | ||
|
||
private void AirspaceButton_Click(object sender, RoutedEventArgs e) | ||
{ | ||
status.Content = "AirspaceClicked"; | ||
} | ||
|
||
private void AddressBar_GotFocus(object sender, RoutedEventArgs e) | ||
{ | ||
status.Content = "GotFocus:AddressBar"; | ||
} | ||
|
||
private void GoButton_GotFocus(object sender, RoutedEventArgs e) | ||
{ | ||
status.Content = "GotFocus:GoButton"; | ||
} | ||
|
||
private void ToggleButton_GotFocus(object sender, RoutedEventArgs e) | ||
{ | ||
status.Content = "GotFocus:ToggleButton"; | ||
} | ||
|
||
private void AirspaceButton_GotFocus(object sender, RoutedEventArgs e) | ||
{ | ||
status.Content = "GotFocus:AirspaceButton"; | ||
} | ||
} | ||
|
||
} | ||
|
17 changes: 17 additions & 0 deletions
17
...2WpfCompositionControl/WebView2WpfCompositionControl/WebView2WpfCompositionControl.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFrameworks>net8.0-windows10.0.22000.0;netcoreapp3.0</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UseWPF>true</UseWPF> | ||
<LangVersion>10</LangVersion> | ||
<Configurations>Debug</Configurations> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.*-*" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.