自助推荐
立即入驻

WPF中WebBrowser不显示脚本错误的方法之vb.net

博客3年前更新 桔子雨
2,228 00
WPF中WebBrowser不显示脚本错误的方法之vb.net

在winform中WebBrowser不显示脚本错误可以用

webBrowser1.ScriptErrorsSuppressed = true

需要注意的是上面代码在.net framework里测试的。在.net 6中没有webBrowser这个控件所以我没测试。那么在wpf里面如何不显示脚本错误呢?代码如下:

1、xaml部分

    <Grid>
        <DockPanel>
            <DockPanel DockPanel.Dock="Top" Height="80">
                <Button Content="打开百度" Click="Button_Click" HorizontalAlignment="Left" Height="66" Margin="350,5,0,0" VerticalAlignment="Top" Width="101"/>
            </DockPanel>
            <WebBrowser x:Name="web" Navigated="web_Navigated" Margin="0,0,0,0"></WebBrowser>

        </DockPanel>
    </Grid>

2、vb.net部分

    Private Sub SetWebBrowserSilent(ByVal webBrowser As WebBrowser, ByVal silent As Boolean)
        Dim fi As System.Reflection.FieldInfo = GetType(WebBrowser).GetField("_axIWebBrowser2", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)

        If fi IsNot Nothing Then
            Dim browser As Object = fi.GetValue(webBrowser)
            If browser IsNot Nothing Then browser.[GetType]().InvokeMember("Silent", System.Reflection.BindingFlags.SetProperty, Nothing, browser, New Object() {silent})
        End If
    End Sub

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        web.Navigate("https://www.baidu.com")
    End Sub

    Private Sub web_Navigated(sender As Object, e As NavigationEventArgs)
        SetWebBrowserSilent(sender, True)
    End Sub
© 版权声明

相关文章