首页 > 其他分享 >WPF WebBrowser suppress script errors

WPF WebBrowser suppress script errors

时间:2024-09-18 18:23:42浏览次数:1  
标签:errors Windows suppress objComBrowser System blog WebBrowser using WPF

Script Error,An error has occured in the script on this page. Do you want to continue running scripts on this page?

 

 

 

//xaml
<Window x:Class="WpfApp378.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp378"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <WebBrowser Source="http://www.tencent.com" x:Name="webBrowser" />
    </Grid>
</Window>



//cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp378
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SuppressScriptErrors(webBrowser, true);
        }

        void SuppressScriptErrors(WebBrowser browser, bool hide)
        {
            browser.Navigating += (s, e) =>
            {
                var fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2",
                    System.Reflection.BindingFlags.Instance |
                    System.Reflection.BindingFlags.NonPublic);
                if (fiComWebBrowser == null)
                {
                    return;
                }

                object objComBrowser = fiComWebBrowser.GetValue(browser);
                if (objComBrowser == null)
                {
                    return;
                }
                objComBrowser.GetType().InvokeMember("Silent",
                    System.Reflection.BindingFlags.SetProperty,
                    null,
                    objComBrowser, new object[] { hide });
            };
        }
    }
}

 

 

 

 

 

 

 

 

 

 

Copy solution from 

https://blog.csdn.net/weixin_33965305/article/details/86170436?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-3-86170436-blog-106261340.235%5Ev43%5Epc_blog_bottom_relevance_base5&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-3-86170436-blog-106261340.235%5Ev43%5Epc_blog_bottom_relevance_base5&utm_relevant_index=6

标签:errors,Windows,suppress,objComBrowser,System,blog,WebBrowser,using,WPF
From: https://www.cnblogs.com/Fred1987/p/18419066

相关文章

  • wpf简单自定义控件
    用户控件(UserControl)和自定义控件(CustomControl)的区别:UserControl:将多个WPF控件(例如:TextBox,TextBlock,Button)进行组合成一个可复用的控件组;由XAML和CodeBehind代码组成;不支持样式/模板重写;CustomControl自定义控件,扩展自一个已经存在的控件,并添加新的功能/特性;由C......
  • WPF CheckBox ToolTip Image
    <Windowx:Class="WpfApp377.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF RadioButton GroupName
    <Windowx:Class="WpfApp375.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF LiveChart 图表详解
    引用LiveChart.Wpf在使用的界面当中引用LiveChart.Wpf的类库xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"以直方图、折线图为例,都属于CartesianChart下的一种Series类型,例如折线图,如下:<lvc:CartesianChart><lvc:CartesianCh......
  • WPF overlay on the icon in the taskbar via TaskbarItemInfo
    <Windowx:Class="WpfApp372.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF打包独立运行的程序
    方案一:使用Costura.Fody插件将自己写的程序打包成一个可以独立运行的EXE文件第1步:安装Costura.Fody首先用VisualStudio2017打开你的解决方案,依次找到“工具”---“NuGet包管理”-“管理解决方案的NuGet程序包”,到了这一步会打开NuGet-解决方案页面,在浏览选项下面的搜索框内......
  • WPF入门教学二 安装与配置WPF开发环境
    在安装与配置WPF(WindowsPresentationFoundation)开发环境时,您需要遵循一系列步骤来确保一切顺利进行。WPF是微软提供的一个强大的UI框架,用于构建Windows桌面应用程序。以下是详细的安装与配置指南:安装VisualStudio访问官方网站:访问MicrosoftVisualStudio官网,下载并安装适......
  • WPF JumpList JumpList ApplicationPath IconResourcePath
    //App.xaml<Applicationx:Class="WpfApp369.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local=&......
  • WPF DataGrid ContextMenu CommandParameter Relative x:Type ContextMenu ,Path=Plac
    //xaml<DataGrid.ContextMenu><ContextMenu><MenuItemHeader="SerializeBinary"Command="{BindingBinSerializeCmd}"CommandParameter="{BindingRelativeSource={Relativ......
  • WPF this.DragMove() DropShadowEffect
    //xaml<Windowx:Class="WpfApp367.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......