Repository navigation
Description
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 secondNavigate()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
refreshorhistory.pushStateparts, mentioned inSourceChangedMSDN 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
CoreWebView2NavigationCompletedEventArgssomeUriproperty to get the actual URL there - Make
CoreWebView2change itsSourcewhenNavigate()was called - Give
CoreWebView2some possibility to reach its "Parent"/Controller/HostingUI (WinFormsWebView2or WPFWebView2)
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
CoreWebView2in a separate .NET library project (no WinForms dependencies) - This means no WinForms
WebView2is involved there per se - But the used
CoreWebView2is coming from theCoreWebView2property of a WinFormsWebView2 - It is constructor-injected into the separate .NET library project
await WebView2.EnsureCoreWebView2Async()is called before using theCoreWebView2
OS:
Windows 10 Pro 21H2 Build 19044.2846
Repro Steps
- All named calls/events/properties here are related to
CoreWebView2class (not theWebView2class). - Register on
SourceChangedevent. - Call
Navigate()method, with URL#1 to navigate to. - Call
Navigate()method, with URL#2 to navigate to. - Result1:
Sourceproperty has not changed. - Result2:
SourceChangedevent 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.
Activity
plantree commentedon May 17, 2023
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
SourceandSourceChangedevent is raised.If you're available, could you provide a simple
.slnfor reproducing? Thanks!mbodm commentedon May 18, 2023
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:
Microsoft.Web.WebView2NuGet 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.csfile. 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 theNavigationCompletedhandler. 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.csfile. 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!
plantree commentedon Jun 5, 2023
Hi MBODM (@mbodm),
Sorry for my late reply. I have tried the demo you provided, which is very rigorous.
There is a
Sourceproperty inCoreWebView2, but it seems that it'll be updated after the whole navigation, which means, duringNavigationCompleted, 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.
mbodm commentedon Jun 5, 2023
Hi plantree (@plantree),
thanks for your kind reply!
Exactly.
I’m curious to see what happens next. But don’t stress yourself, it's not in a hurry. 😄
plantree commentedon Jun 6, 2023
Hi MBODM (@mbodm),
I have put this into our backlog, and will synchronize with you ASAP :) .
monica-ch commentedon Jun 18, 2024
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