简体   繁体   English

如何在c#中的wpf webbrowser控件中注入js文件

[英]How to inject js file in wpf webbrowser control in c#

Hi I am using WPF WebBrowser control on my xaml page. 嗨,我在我的xaml页面上使用WPF WebBrowser控件。 I have Html,Jquery,js, and css file in my html package. 我的html包中有Html,Jquery,js和css文件。 I have to load html page in web browser control with all jquery library. 我必须在所有jquery库的Web浏览器控件中加载html页面。 How can I inject this file? 我怎么能注入这个文件? I am writing code like this: 我正在编写这样的代码:

page.xaml.cs page.xaml.cs

 void wb_Loaded(object sender, RoutedEventArgs e)
    {
        string htmlPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "keyboardWidget\\test.html");
        string html = File.ReadAllText(htmlPath.ToString());
        webBrowser.NavigateToString(html);

    }

this is page.xaml: 这是page.xaml:

<WebBrowser x:Name="webBrowser"></WebBrowser>

I am getting error Like this. 我收到这样的错误。

在此输入图像描述

Hi can anyone suggest what is the best way to inject js or jquery library. 嗨,任何人都可以建议什么是注入js或jquery库的最佳方法。 if it's not possible in wpf web browser control then what control I should use. 如果在wpf web浏览器控件中不可能那么我应该使用什么控件。

I suspect the problem might not be that the JS script was not found by the WebBrowser control, but that the WPF WebBrowser control has compatibility issues with JSON. 我怀疑问题可能不是WebBrowser控件找不到JS脚本,而是WPF WebBrowser控件与JSON存在兼容性问题。

Try including this script in your page and see if it resolves the problem: JSON2 尝试在您的页面中包含此脚本,看看它是否可以解决问题: JSON2

Download the json2.js and include this in the HTML of your page: 下载json2.js并将其包含在您网页的HTML中:

<script  type="text/javascript" src="json2.js"></script>

I was not able to render html with js file in WPF WebBrowser control. 我无法在WPF WebBrowser控件中使用js文件呈现html。

Then I have used GeckoFx Control. 然后我使用了GeckoFx Control。

Add xulrunner ,Geckofx-Winforms.dll and Geckofx-Core.dll then I had added page.xaml 添加xulrunner,Geckofx-Winforms.dll和Geckofx-Core.dll然后我添加了page.xaml

 <Grid x:Name="gecoBrowser" Height="150" Width="735">

page.xaml.cs: page.xaml.cs:

 Gecko.Xpcom.Initialize(AppDomain.CurrentDomain.BaseDirectory + "xulrunner");
            Gecko.GeckoWebBrowser myBrowser = new Gecko.GeckoWebBrowser();
            WindowsFormsHost host = new WindowsFormsHost();
            host.Child = myBrowser;
            gecoBrowser.Children.Add(host);
            string htmlPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "keyboardWidget\\test.html");
            myBrowser.Navigate("file:///" + htmlPath);

Hi found the answer of this Question : inject js file in wpf web browser control. 您好找到了这个问题的答案:在wpf web浏览器控件中注入js文件。

first you have to update your IE in html like in head of html: 首先你必须在html的头部更新你的IE浏览器:

 <meta http-equiv="x-ua-compatible" content="IE=9">

inject this jquery library:- 注入这个jquery库: -

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

The latter: 后者:

https://forum.jquery.com/topic/json-is-undefined-with-jquery-min-2-0-3-on-ie-11 https://forum.jquery.com/topic/json-is-undefined-with-jquery-min-2-0-3-on-ie-11

IE 11 mode registry: IE 11模式注册表:

https://weblog.west-wind.com/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version https://weblog.west-wind.com/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM