Skip to content

The CoreWebView2.Navigate() method not changes Source property or raise SourceChanged event #3461

Closed

Description

@mbodm

Description

When calling Navigate() method of CoreWebView2 class (not WebView2 class), the Source property of CoreWebView2 class not changes. Also the SourceChanged event of CoreWebView2 class is not raised.

Note:

  • The URL used for a first Navigate() call is different to the URL used for a second Navigate() call.
  • Both URLs are normal websites, not local HTML files, or something like that.
  • Both URLs have the same domain, but path and query are completely different.
  • Both URLs are not affected by the refresh or history.pushState parts, mentioned in SourceChanged MSDN comment.

Background

I need to get the actual URL in the NavigationCompleted event handler, since there is no Uri property in its CoreWebView2NavigationCompletedEventArgs, like i.e. the NavigationStarting event has (e.Uri). Therefore i need to use the Source property of CoreWebView2 there instead, to have knowledge about what the actual URL is.

I have no clue why Microsoft decided to give the CoreWebView2NavigationCompletedEventArgs no Uri property... ?!?!

Suggestion

Form a simple developer point of view:

  • Give CoreWebView2NavigationCompletedEventArgs some Uri property to get the actual URL there
  • Make CoreWebView2 change its Source when Navigate() was called
  • Give CoreWebView2 some possibility to reach its "Parent"/Controller/HostingUI (WinForms WebView2 or WPF WebView2)

These 3 things i missed the most, when i worked with WebView2 the last 70-80 hours. But this is just my personal experience.

Version

SDK:
Microsoft.Web.WebView2 NuGet package version 1.0.1722.45

Runtime:
Edge Version 112.0.1722.68 (Official Build) (64-Bit)

Framework:
WinForms and .NET 6

  • But i am using solely CoreWebView2 in a separate .NET library project (no WinForms dependencies)
  • This means no WinForms WebView2 is involved there per se
  • But the used CoreWebView2 is coming from the CoreWebView2 property of a WinForms WebView2
  • It is constructor-injected into the separate .NET library project
  • await WebView2.EnsureCoreWebView2Async() is called before using the CoreWebView2

OS:
Windows 10 Pro 21H2 Build 19044.2846

Repro Steps

  • All named calls/events/properties here are related to CoreWebView2 class (not the WebView2 class).
  • Register on SourceChanged event.
  • Call Navigate() method, with URL#1 to navigate to.
  • Call Navigate() method, with URL#2 to navigate to.
  • Result1: Source property has not changed.
  • Result2: SourceChanged event handler was not called.

Additional context

It is important to understand we are talking solely about the CoreWebView2 class and not about the WebView2 class. Since for the later one the Source property is changed indeed.

AB#47716077

Activity

  1. added
    bug
    on May 6, 2023
  2. plantree commented on May 17, 2023

    @plantree
    Contributor

    Hi MBODM (@mbodm),

    Thanks for your clear and accurate description!

    I have tested your way, and everything seems fine: I could get the up-to-date Source and SourceChanged event is raised.

    If you're available, could you provide a simple .sln for reproducing? Thanks!

  3. mbodm commented on May 18, 2023

    @mbodm
    Author

    plantree (@plantree)

    Hi,

    thanks for the fast reply! I´ve created an example (very small and basic), to demonstrate the behavior: https://github.com/MBODM/CoreWebView2SourceProblem

    Used environment/stuff:

    • Windows 10
    • Visual Studio 2022 (Version 17.5.5)
    • Solution contains a simple .NET 6 WinForms project
    • Solution contains a simple .NET 6 ClassLibrary project
    • Both projects use the Microsoft.Web.WebView2 NuGet package (Version 1.0.1774.30)

    Search for the word "issue" by using Visual Studio´s Find in files (STRG+SHIFT+F) method, to quickly navigate to the position, where the actual behavior differs from the expected behavior.

    Hint:
    All the interessting stuff happens inside the WebViewManager.cs file. All the rest is just a little bif of "clue code" here.

    Notes:
    The root of the problem may be the fact, that i´m calling CoreWebView2.Navigate() inside the NavigationCompleted handler. As some starting point for you. When you analyzed/understood the given code, you will realize that the reason why the program not stops (requests are restarted infinitely) is based on the main-problem ("Source not changes"). Just for consideration.

    I also added a workaround, so you can clearly see the difference in behavior. If you just start the application, you will realize above mentioned infinetly started requests. Now go to line 66 in WebViewManager.cs file. Just out-comment this line (add a //) and un-comment next line (line 67), by removing the // in line 67. Now you will see: Everything works fine. After you analyzed/understood what this means, you should understand the problem with ease.

    Luckily, by accident, the following commit shows exactly above description:
    mbodm/CoreWebView2SourceProblem@56158d2

    If you need further information or data, just reply here.
    Best wishes and thanks a lot for offering some help!

  4. plantree commented on Jun 5, 2023

    @plantree
    Contributor

    Hi MBODM (@mbodm),

    Sorry for my late reply. I have tried the demo you provided, which is very rigorous.

    There is a Source property in CoreWebView2, but it seems that it'll be updated after the whole navigation, which means, during NavigationCompleted, it still holds the old value.

    As for your feature request, we'll have a discussion internally. I'll synchronize with you as soon as there is progress.

  5. mbodm commented on Jun 5, 2023

    @mbodm
    Author

    Hi plantree (@plantree),

    thanks for your kind reply!

    There is a Source property in CoreWebView2, but it seems that it'll be updated after the whole navigation, which means, during NavigationCompleted, it still holds the old value.

    Exactly.

    I’m curious to see what happens next. But don’t stress yourself, it's not in a hurry. 😄

  6. plantree commented on Jun 6, 2023

    @plantree
    Contributor

    Hi MBODM (@mbodm),

    I have put this into our backlog, and will synchronize with you ASAP :) .

  7. added
    tracked
    on Nov 17, 2023
  8. monica-ch commented on Jun 18, 2024

    @monica-ch
    Contributor

    MBODM (@mbodm) From the demo project, I see it is navigating to a site that triggers Download so SourceChanged event doesn't fires nor WebView2 Source gets updated. This is by design that if WV2 is trying to navigate to a site that invokes Downloads the navigation completes and initiates DownloadStarting event. You can listen to DownloadStarting event and get the URI from the DownloadOperation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugtracked

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions