Description
Setup
- WPF application
- .Net 4.7.2
- 0.9.579-prerelease package for WebView
- Edge Dev installed
Problem
I have found some problems with links in pdf's displayed in WPF with the new WebView. I have a listener on NavigationStarting which then uses IsUserInitiated to determine if the program or the user made the navigation. If it was the latter (the user) I then open the link externally.
-
When the user clicks on a normal http:// link the CoreWebView2NavigationStartingEventArgs will report that the IsUserInitiated is false.
I expected this to be true. -
When a link points to an external protocol myProtocol://somewhere the NavigationStarting handler is never raised, and nothing happens. If you right-click on the link and select "Open link in new window" a new empty window will spawn, and then ask if you want to open the link in the external program.
I excepted this to either hit the NavigationStarting or PermissionRequested handlers. -
When opening a link via 'Open in new window' in the context menu the NewWindowRequested handler does not fire.
I expected that NewWindowRequested was raised.
None of these problems are present when displaying webpages.
Code
MainWindow.xaml
<Window x:Class="TestWebView2App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="1000">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Content="Navigate back to pdf"
Click="Button_Click" />
<wv2:WebView2 Name="WebView"
Grid.Row="1"
Source="locationOfLinkExamples.pdf" />
<ScrollViewer Height="150"
Grid.Row="2">
<TextBox x:Name="WebViewEvents"
Text="*** EVENT OVERVIEW ***"
Foreground="OrangeRed"
Background="Black"
IsReadOnly="True" />
</ScrollViewer>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Windows;
namespace TestWebView2App
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WebView.CoreWebView2Ready += WebView_CoreWebView2Ready;
}
private void WebView_CoreWebView2Ready(object sender, EventArgs e)
{
WebView.CoreWebView2.NavigationStarting += CoreWebView2_WebView_NavigationStarting;
WebView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
WebView.CoreWebView2.PermissionRequested += CoreWebView2_PermissionRequested;
pdfAddress = WebView.Source;
}
private void CoreWebView2_PermissionRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2PermissionRequestedEventArgs e)
{
WebViewEvents.Text += Environment.NewLine + $"Requesting permission for '{e.Uri}', is from user: '{e.IsUserInitiated}'";
}
private void CoreWebView2_NewWindowRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
{
WebViewEvents.Text += Environment.NewLine + $"New window requested for '{e.Uri}', is from user: '{e.IsUserInitiated}'";
}
private void CoreWebView2_WebView_NavigationStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs e)
{
WebViewEvents.Text += Environment.NewLine + $"Navigation to '{e.Uri}', is from user: '{e.IsUserInitiated}'";
}
private Uri pdfAddress = new Uri("http://google.com");
private void Button_Click(object sender, RoutedEventArgs e)
{
WebView.Source = pdfAddress;
}
}
}
How to reproduce
- Download the example pdf
- Change the path of locationOfLinkExamples.pdf to wherever the pdf is located
- Start the program
- Click the first link, observe that IsUserInitiated is false
- Click the second link, observe that no events are sent
- Right click the first link and select 'Open link in new window',
observe that no events are sent even though a new window opens - Right click the second link and select 'Open link in new window',
observe that no events are sent even though a new window opens
OBS: On my computer the protocol mp is associated with an application and works without problem when accessed through the edge browser normally.