首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Webview2事件发生后停止重新加载NavigationCompleted网站。VB.NET C#

如何在Webview2事件发生后停止重新加载NavigationCompleted网站。VB.NET C#
EN

Stack Overflow用户
提问于 2022-01-13 09:43:00
回答 1查看 420关注 0票数 1

我有一个用Webview2浏览器打开表单的模块。然后,Webview2控件使用用户用户名和密码(他在前面输入到两个文本框中)登录到一个网站,并通过几个链接加载到特定的网页,用户可以在其中搜索客户,www.login.ca/search。在用户输入名称并单击“搜索”按钮后,下一个页面加载一秒钟,然后再次加载www.login.ca/search页面。我只想让用户能够在不需要浏览器重新加载"login.ca/search“网页的情况下继续浏览该页面。

我有这个:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-13 13:52:55

FirstPageWebView的第一行应该再次删除处理程序,因为它只应该使用一次。

当您调用AddHandler时,您需要注册2个处理程序,它们将在每个NavigationCompleted事件上同时运行。

(我不知道Vb.Net,所以这段代码可能是错误的,但我希望你能理解:

代码语言:javascript
运行
复制
Private Async Sub FirstPageWebView(ByVal sender As Object, ByVal e As CoreWebView2NavigationCompletedEventArgs)
    RemoveHandler(WebviewForm.WebView21.NavigationCompleted), AddressOf LoginWebpageLoaded
    ......... more code .........
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70694361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档