简体   繁体   English

C#WebBrowser对象中的HTML / Javascript文件的本地加载未正确加载?

[英]Local load of HTML/Javascript file in C# WebBrowser object not loading correctly?

I'm using .NET Framework 4.0 (with WPF) trying to load a local HTML file within a WebBrowser object (System.Windows.Controls.WebBrowser) with both locally embedded javascript and loaded from a remote server. 我正在使用.NET Framework 4.0(带有WPF),试图通过本地嵌入的javascript并从远程服务器加载WebBrowser对象(System.Windows.Controls.WebBrowser)内的本地HTML文件。 The problem is, the javascript (ajax with dojo) isn't executing inside the WebBrowser object when loaded: 问题是,加载时javascript(带有dojo的ajax)未在WebBrowser对象内执行:

webBrowser.NavigateToString(LoadStringFromFile("map.html"));

However, when loaded remotely it runs just fine as follows: 但是,在远程加载时,它可以按如下所示正常运行:

webBrowser.Navigate("http://www.example.com/map.html");

Sample excerpt code of Javascript code in html file: html文件中的Javascript代码示例摘录代码:

<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>
<script type="text/javascript">
dojo.require("esri.map");

var map;

var colorRGB = { "white": [255,255,255], "red": [255,0,0], "blue": [0,255,0] };

function init()
{
    var streetLayer = new esri.layers.ArcGISTiledMapServiceLayer(
        "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");

    var extent = new esri.geometry.Extent(-140.910, 11.267, -53.019, 64.002);
    map = new esri.Map("map", { extent:extent });

    map.addLayer(streetLayer);

    dojo.connect(map,"onLoad", processLocations);
}

Anyone have any idea what is wrong with this? 任何人都知道这有什么问题吗?

My research suggest it has to do with IE and internet zones, but have been unable to confirm it. 我的研究表明,它与IE和Internet区域有关,但无法确认。

The WPF WebBrowser class has a static initializer that enables FEATURE_LOCALMACHINE_LOCKDOWN for the entire process. WPF WebBrowser类具有一个静态初始化程序,该初始化程序为整个过程启用FEATURE_LOCALMACHINE_LOCKDOWN You can use CoInternetSetFeatureEnabled to turn FEATURE_LOCALMACHINE_LOCKDOWN back off on the WebBrowser Navigating event. 您可以使用CoInternetSetFeatureEnabled在WebBrowser导航事件上关闭FEATURE_LOCALMACHINE_LOCKDOWN

This solution does not require you to modify the HTML files being displayed, but does disable some security features that you probably don't want when displaying trusted local content. 此解决方案不需要您修改正在显示的HTML文件,但是会禁用一些在显示受信任的本地内容时可能不需要的安全功能。 See http://technet.microsoft.com/en-us/library/cc782928(v=ws.10).aspx for more information about local machine lockdown. 有关本地计算机锁定的更多信息,请参见http://technet.microsoft.com/zh-cn/library/cc782928(v=ws.10).aspx

See How to disable click sound in WebBrowser Control for PInvoke and https://github.com/TaoK/PoorMansTSqlFormatter/blob/d6b4f7bedc02ce1bf59acb16dd1f49609c216aa7/PoorMansTSqlFormatterDemo/FrameworkClassReplacements/CustomContentWebBrowser.cs for an example usage. 有关示例,请参见如何在Web浏览器控件中禁用 PInvoke的点击声音https://github.com/TaoK/PoorMansTSqlFormatter/blob/d6b4f7bedc02ce1bf59acb16dd1f49609c216aa7/PoorMansTSqlFormatterDemo/FrameworkClassReplacements/CustomContentWebBrowser.cs的示例。

When i paste this into an html file and open on internet explorer, it display a message that it blocked activex/script content. 当我将其粘贴到html文件中并在Internet Explorer上打开时,它显示一条消息,表明它阻止了ActiveX /脚本内容。 Change Internet Options > Advanced > Security > Allow active content to run in files on My Computer. 更改Internet选项>高级>安全性>允许活动内容在“我的电脑”上的文件中运行。 If the warning goes away in IE, then it should work in WebBrowser object 如果警告在IE中消失,则它应该在WebBrowser对象中起作用

Here is my solution for preventing scriptwarnings: 这是我用于防止脚本警告的解决方案:

WebBrowser.ScriptErrorsSuppressed = true;

Also I've put my website as a trusted website in my Internet Settings, I don't know if that helps but the scriptErrorsSuppressed property was enough for me. 另外,我已经将网站作为受信任的网站放置在“ Internet设置”中,我不知道这是否有帮助,但是scriptErrorsSuppressed属性对我来说已经足够了。

Hope this helps. 希望这可以帮助。

For me, the problem was: 对我来说,问题是:

Steps to enable ActiveX controls in Internet Explorer: 在Internet Explorer中启用ActiveX控件的步骤:

1.Select Tools --> Internet Options menu from the Internet Explorer. 1.从Internet Explorer中选择工具-> Internet选项菜单。

2.Select the Security tab from the Internet Options dialog. 2.从“ Internet选项”对话框中选择“安全性”选项卡。

3.Select the appropriate Web content zone and click Custom Level. 3.选择适当的Web内容区域,然后单击“自定义级别”。

4.Make the following options available under ActiveX controls and plug-ins to either enableor Prompt:1.Download signed ActiveX controls 4,使ActiveX控件和插件下的以下选项可用于启用程序或提示:1.下载签名的ActiveX控件

2.Run ActiveX controls and plug-ins 2.运行ActiveX控件和插件

3.Script ActiveX controls marked safe for scripting 3.Script ActiveX控件标记为可以安全编写脚本

5.Click Ok to save the security settings. 5.单击确定保存安全设置。

6.Click OK to save and close the Internet Options dialog. 6.单击“确定”保存并关闭“ Internet选项”对话框。

Then click don't show this message again. 然后,单击不再显示此消息。

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

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