如何在 NavigationCompleted 事件后停止 Webview2 重新加载网站。 VB.NET C#
How to stop Webview2 reloading website after NavigationCompleted event. VB.NET C#
我有一个使用 Webview2 浏览器打开表单的模块。 Webview2 控件然后使用用户用户名和密码(他之前在两个文本框中输入)登录网站并通过几个链接加载到特定网页,用户可以在其中搜索客户,再次 www.login.ca/search. After the user enters a name and clicks on the search button the next page load for a second but then loads the www.login.ca/search 页面。我只想让用户能够继续浏览该页面,而无需浏览器重新加载“登录。ca/search”网页。
我有这个:
Imports System.Windows.Interop
Imports Microsoft.Web.WebView2.Core
Module SearchForCustomer
Public Sub CheckCustomer()
WebviewForm.Show()
OpenAndLogIn()
End Sub
Public Sub OpenAndLogIn()
'Open webpage and log in
Dim Loginwebaddress As String = "https://www.login.ca"
WebviewForm.WebView21.Source = New Uri(Loginwebaddress)
AddHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf FirstPageWebView
End Sub
Private Async Sub FirstPageWebView(ByVal sender As Object, ByVal e As CoreWebView2NavigationCompletedEventArgs)
Dim msg = TextBox1.Text 'Username
Dim msg2 = TextBox2.Text 'password
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('LoginUN').value = '{msg}';")
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('Login1PW').value = '{msg2}';")
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('Login1_DoLogin').click();")
AddHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf LoginWebpageLoaded
End Sub
Private Sub LoginWebpageLoaded
'Load the search for customer page
Dim Loginwebaddress As String = "https://www.login.ca/search"
WebviewForm.WebView21.Source = New Uri(Loginwebaddress)
End Sub
FirstPageWebView
的第一行应该再次删除处理程序,因为它只能使用一次。
当您调用 AddHandler
时,您实际上注册了 2 个处理程序,并且它们将在每个 NavigationCompleted
事件中都 运行。
(我不知道 Vb.Net 所以这段代码可能有误,但我希望你明白了:
Private Async Sub FirstPageWebView(ByVal sender As Object, ByVal e As CoreWebView2NavigationCompletedEventArgs)
RemoveHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf LoginWebpageLoaded
......... more code .........
我有一个使用 Webview2 浏览器打开表单的模块。 Webview2 控件然后使用用户用户名和密码(他之前在两个文本框中输入)登录网站并通过几个链接加载到特定网页,用户可以在其中搜索客户,再次 www.login.ca/search. After the user enters a name and clicks on the search button the next page load for a second but then loads the www.login.ca/search 页面。我只想让用户能够继续浏览该页面,而无需浏览器重新加载“登录。ca/search”网页。
我有这个:
Imports System.Windows.Interop
Imports Microsoft.Web.WebView2.Core
Module SearchForCustomer
Public Sub CheckCustomer()
WebviewForm.Show()
OpenAndLogIn()
End Sub
Public Sub OpenAndLogIn()
'Open webpage and log in
Dim Loginwebaddress As String = "https://www.login.ca"
WebviewForm.WebView21.Source = New Uri(Loginwebaddress)
AddHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf FirstPageWebView
End Sub
Private Async Sub FirstPageWebView(ByVal sender As Object, ByVal e As CoreWebView2NavigationCompletedEventArgs)
Dim msg = TextBox1.Text 'Username
Dim msg2 = TextBox2.Text 'password
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('LoginUN').value = '{msg}';")
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('Login1PW').value = '{msg2}';")
Await WebviewForm.WebView21.ExecuteScriptAsync($"document.getElementById('Login1_DoLogin').click();")
AddHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf LoginWebpageLoaded
End Sub
Private Sub LoginWebpageLoaded
'Load the search for customer page
Dim Loginwebaddress As String = "https://www.login.ca/search"
WebviewForm.WebView21.Source = New Uri(Loginwebaddress)
End Sub
FirstPageWebView
的第一行应该再次删除处理程序,因为它只能使用一次。
当您调用 AddHandler
时,您实际上注册了 2 个处理程序,并且它们将在每个 NavigationCompleted
事件中都 运行。
(我不知道 Vb.Net 所以这段代码可能有误,但我希望你明白了:
Private Async Sub FirstPageWebView(ByVal sender As Object, ByVal e As CoreWebView2NavigationCompletedEventArgs)
RemoveHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf LoginWebpageLoaded
......... more code .........