i am creating a desktop application with Webview2 as browser. I have to change the user agent to get data from a website. My control name is Webview21 for the browser control. I got the error message System.NotImplementedException: 'Unable to cast to Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Settings2. This may happen if you are using an interface not supported by the version of the WebView2 Runtime . But i have the latest evergreen version. What is the solution?
Public Async Sub InitializeBrowser()
Dim strip As String = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62 seakayak-navigator"
Dim opts As New CoreWebView2EnvironmentOptions With {.AdditionalBrowserArguments = "--disable-web-security --allow-file-access-from-files --allow-file-access "}
Dim userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\SeakayakNavigator"
Dim env = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder)
Await WebView21.EnsureCoreWebView2Async(env)
Application.DoEvents()
WebView21.Width = Fmenu.Width
WebView21.Height = Fmenu.Height
WebView21.Top = 0
WebView21.Left = 0
WebView21.BringToFront()
WebView21.CoreWebView2.Settings.UserAgent = strip
' Add any initialization after the InitializeComponent() call.
AddHandler WebView21.NavigationCompleted, AddressOf webview21_navigationCompleted
WebView21.CoreWebView2.AddHostObjectToScript("Bridge", New Bridge())
End Sub
CoreWebView2.Settings.UserAgent
is not supported in the current stable release (version 1.0.818.41).
If you want to use CoreWebView2.Settings.UserAgent
you must install the pre-release version, currently version 1.0.865-prerelease.
You can follow the directions here: To install a pre-release version
That also means that you must use Microsoft Edge CANARY build which you can get here: Microsoft Edge Insider Channels
Unfortunately, you might have to uninstall the 'WebView2 runtime' (which I think is a bug in the WebView2 package).
Update:
Now the latest stable version supports setting the
useragent
.