MicrosoftEdge / MicrosoftEdge/WebView2Feedback

PDF View is not raising the correct events for links

Open
#239 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

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.

  1. When the user clicks on a normal http:// link the CoreWebView2NavigationStartingEventArgs will report that the IsUserInitiated is false.
    I expected this to be true.

  2. 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.

  3. 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
  1. Download the example pdf
  2. Change the path of locationOfLinkExamples.pdf to wherever the pdf is located
  3. Start the program
  4. Click the first link, observe that IsUserInitiated is false
  5. Click the second link, observe that no events are sent
  6. Right click the first link and select 'Open link in new window',
    observe that no events are sent even though a new window opens
  7. 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.

LinkExamples.pdf

AB#28648750

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the event handlers in MainWindow.xaml.cs and the WebView2 setup in MainWindow.xaml, then reproduce the three PDF-link cases using LinkExamples.pdf. Done means the documented PDF link actions raise the expected NavigationStarting, PermissionRequested, or NewWindowRequested events with the correct user-initiated state.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.