Skip to content

Commit cd44c00

Browse files
committed
Move wpf sample to use latest WebView2 prerelease SDK 0.9.579
1 parent 1da5c3d commit cd44c00

File tree

5 files changed

+142
-4
lines changed

5 files changed

+142
-4
lines changed

SampleApps/WebView2WpfBrowser/MainWindow.xaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@ found in the LICENSE file.
2525
<CommandBinding Command="NavigationCommands.Refresh" Executed="RefreshCmdExecuted" CanExecute="RefreshCmdCanExecute"/>
2626
<CommandBinding Command="NavigationCommands.BrowseStop" Executed="BrowseStopCmdExecuted" CanExecute="BrowseStopCmdCanExecute"/>
2727
<CommandBinding Command="NavigationCommands.GoToPage" Executed="GoToPageCmdExecuted" CanExecute="GoToPageCmdCanExecute"/>
28+
<CommandBinding Command="NavigationCommands.IncreaseZoom" Executed="IncreaseZoomCmdExecuted" CanExecute="IncreaseZoomCmdCanExecute"/>
29+
<CommandBinding Command="NavigationCommands.DecreaseZoom" Executed="DecreaseZoomCmdExecuted" CanExecute="DecreaseZoomCmdCanExecute"/>
30+
<CommandBinding Command="{x:Static local:MainWindow.InjectScriptCommand}" Executed="InjectScriptCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
2831
</Window.CommandBindings>
2932
<DockPanel>
3033
<Menu DockPanel.Dock="Top">
3134
<MenuItem Header="_File">
3235
<MenuItem Header="_New Window" Command="ApplicationCommands.New"/>
3336
<MenuItem Header="_Close" Command="ApplicationCommands.Close"/>
37+
<MenuItem Header="_Inject Script..." Command="{x:Static local:MainWindow.InjectScriptCommand}"/>
38+
</MenuItem>
39+
<MenuItem Header="_View">
40+
<MenuItem Header="_Increase Zoom" Command="NavigationCommands.IncreaseZoom"/>
41+
<MenuItem Header="_Decrease Zoom" Command="NavigationCommands.DecreaseZoom"/>
3442
</MenuItem>
3543
</Menu>
3644
<DockPanel DockPanel.Dock="Top">
@@ -39,7 +47,8 @@ found in the LICENSE file.
3947
<Button DockPanel.Dock="Left" Command="NavigationCommands.Refresh">Refresh</Button>
4048
<Button DockPanel.Dock="Left" Command="NavigationCommands.BrowseStop">Stop</Button>
4149
<Button DockPanel.Dock="Right" Command="NavigationCommands.GoToPage" CommandParameter="{Binding ElementName=url,Path=Text}">Go</Button>
42-
<TextBox x:Name="url" Text="{Binding ElementName=webView,Path=Source}">
50+
<!-- We want the address bar to update based on the WebView's Source, but we don't want the WebView to navigate just from the user typing into the address bar. Therefore we use the OneWay binding mode. -->
51+
<TextBox x:Name="url" Text="{Binding ElementName=webView,Path=Source,Mode=OneWay}">
4352
<TextBox.InputBindings>
4453
<KeyBinding Key="Return" Command="NavigationCommands.GoToPage" CommandParameter="{Binding ElementName=url,Path=Text}" />
4554
</TextBox.InputBindings>

SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.Linq;
89
using System.Text;
910
using System.Threading.Tasks;
@@ -25,8 +26,10 @@ namespace WebView2WpfBrowser
2526
/// </summary>
2627
public partial class MainWindow : Window
2728
{
29+
public static RoutedCommand InjectScriptCommand = new RoutedCommand();
2830
bool _isNavigating = false;
2931

32+
3033
public MainWindow()
3134
{
3235
InitializeComponent();
@@ -82,14 +85,70 @@ void BrowseStopCmdExecuted(object target, ExecutedRoutedEventArgs e)
8285
webView.Stop();
8386
}
8487

88+
void CoreWebView2RequiringCmdsCanExecute(object sender, CanExecuteRoutedEventArgs e)
89+
{
90+
e.CanExecute = webView != null && webView.CoreWebView2 != null;
91+
}
92+
93+
double ZoomStep()
94+
{
95+
if (webView.ZoomFactor < 1)
96+
{
97+
return 0.25;
98+
}
99+
else if (webView.ZoomFactor < 2)
100+
{
101+
return 0.5;
102+
}
103+
else
104+
{
105+
return 1;
106+
}
107+
}
108+
109+
void IncreaseZoomCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
110+
{
111+
e.CanExecute = webView != null;
112+
}
113+
114+
void IncreaseZoomCmdExecuted(object target, ExecutedRoutedEventArgs e)
115+
{
116+
webView.ZoomFactor += ZoomStep();
117+
}
118+
119+
void DecreaseZoomCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
120+
{
121+
e.CanExecute = (webView != null) && (webView.ZoomFactor - ZoomStep() > 0.0);
122+
}
123+
124+
void DecreaseZoomCmdExecuted(object target, ExecutedRoutedEventArgs e)
125+
{
126+
webView.ZoomFactor -= ZoomStep();
127+
}
128+
129+
async void InjectScriptCmdExecuted(object target, ExecutedRoutedEventArgs e)
130+
{
131+
var dialog = new TextInputDialog(
132+
title: "Inject Script",
133+
description: "Enter some JavaScript to be executed in the context of this page.",
134+
defaultInput: "window.getComputedStyle(document.body).backgroundColor");
135+
if (dialog.ShowDialog() == true) {
136+
string scriptResult = await webView.ExecuteScriptAsync(dialog.Input.Text);
137+
MessageBox.Show(this, scriptResult, "Script Result");
138+
}
139+
}
140+
85141
void GoToPageCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
86142
{
87143
e.CanExecute = webView != null && !_isNavigating;
88144
}
89145

90-
void GoToPageCmdExecuted(object target, ExecutedRoutedEventArgs e)
146+
async void GoToPageCmdExecuted(object target, ExecutedRoutedEventArgs e)
91147
{
92-
webView.Source = new Uri((string)e.Parameter);
148+
// Setting webView.Source will not trigger a navigation if the Source is the same
149+
// as the previous Source. CoreWebView.Navigate() will always trigger a navigation.
150+
await webView.EnsureCoreWebView2Async();
151+
webView.CoreWebView2.Navigate((string)e.Parameter);
93152
}
94153

95154
void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Window x:Class="WebView2WpfBrowser.TextInputDialog"
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:WebView2WpfBrowser"
7+
mc:Ignorable="d"
8+
Title="TextInputDialog"
9+
Height="200"
10+
Width="400">
11+
<Grid>
12+
<Grid.ColumnDefinitions>
13+
<ColumnDefinition Width="*"/>
14+
<ColumnDefinition Width="Auto"/>
15+
<ColumnDefinition Width="Auto"/>
16+
</Grid.ColumnDefinitions>
17+
<Grid.RowDefinitions>
18+
<RowDefinition Height="Auto"/>
19+
<RowDefinition Height="*"/>
20+
<RowDefinition Height="Auto"/>
21+
</Grid.RowDefinitions>
22+
<TextBlock x:Name="Description" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0"/>
23+
<TextBox x:Name="Input" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1"/>
24+
<Button Click="ok_Clicked" IsDefault="True" Grid.Column="1" Grid.Row="2" Width="60">OK</Button>
25+
<Button IsCancel="True" Grid.Column="2" Grid.Row="2" Width="60">Cancel</Button>
26+
</Grid>
27+
</Window>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Windows;
5+
using System.Windows.Controls;
6+
using System.Windows.Data;
7+
using System.Windows.Documents;
8+
using System.Windows.Input;
9+
using System.Windows.Media;
10+
using System.Windows.Media.Imaging;
11+
using System.Windows.Shapes;
12+
13+
namespace WebView2WpfBrowser
14+
{
15+
/// <summary>
16+
/// Interaction logic for TextInputDialog.xaml
17+
/// </summary>
18+
public partial class TextInputDialog : Window
19+
{
20+
public TextInputDialog(
21+
string title = null,
22+
string description = null,
23+
string defaultInput = null)
24+
{
25+
InitializeComponent();
26+
if (title != null) {
27+
Title = title;
28+
}
29+
if (description != null) {
30+
Description.Text = description;
31+
}
32+
if (defaultInput != null) {
33+
Input.Text = defaultInput;
34+
}
35+
Input.Focus();
36+
Input.SelectAll();
37+
}
38+
39+
void ok_Clicked (object sender, RoutedEventArgs args) {
40+
this.DialogResult = true;
41+
}
42+
}
43+
}

SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
<NoWin32Manifest>true</NoWin32Manifest>
1111
</PropertyGroup>
1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.Web.WebView2" Version="0.9.538-prerelease" />
13+
<PackageReference Include="Microsoft.Web.WebView2" Version="0.9.579-prerelease" />
1414
</ItemGroup>
1515
</Project>

0 commit comments

Comments
 (0)