WPF webbrowser 判断网页加载完毕

简介:如果网页没有加载完毕就操作网页,将导致出错,所以需要判断网页是否已经加载好了


控件名称为webbrowser


通过LoadCompleted监听事件来触发函数,这里我直接在界面初始化后进行定义


public MainWindow()
        {
            InitializeComponent();
            this.webbrowser.LoadCompleted += new LoadCompletedEventHandler(webbrowser_LoadCompleted);//添加加载完毕触发函数
            Uri uri = new Uri("https://www.baidu.com/");
            webbrowser.Navigate(uri);

        }


然后是处理函数,当加载完毕后,禁用浏览器的滚动条,并且修改WPF标题

 private void webbrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            
            mshtml.HTMLDocument dom = (mshtml.HTMLDocument)webbrowser.Document; 
            dom.documentElement.style.overflow = "hidden"; //hiden scroller
            dom.body.setAttribute("scroll", "no"); //disable scroller
                this.Title = "Loading complete.";
        }




猜你喜欢

转载自blog.csdn.net/u012388993/article/details/79753568