简体   繁体   English

如何将 HTML/JavaScript 从嵌入式资源加载到 winform Web 浏览器中

[英]How to load HTML/JavaScript from embedded resource into winform web browser

I want to have some HTML files with JavaScript loaded into the web browser control in a winforms (v2.0) application.我想将一些带有 JavaScript 的 HTML 文件加载到 winforms (v2.0) 应用程序中的 Web 浏览器控件中。 During execution, I won't have internet access, so JavaScript and HTML forms will be embedded in he resources.resx file.在执行期间,我将无法访问 Internet,因此 JavaScript 和 HTML 表单将嵌入到 resources.resx 文件中。

1) how can I load an HTML document out of the resource (analogous to a file:/// operation, but it isn't being loaded from the file system), 1) 如何从资源中加载 HTML 文档(类似于 file:/// 操作,但它不是从文件系统加载的),

2) how would I declare the JavaScript scripts to be loaded? 2) 我将如何声明要加载的 JavaScript 脚本? Ie, IE,

<script src=resource.jquery.min.js??? ... />

Thanks!谢谢!

To load the HTML document, just compile your html file as embedded resource, and then:要加载 HTML 文档,只需将您的 html 文件编译为嵌入式资源,然后:

WebBrowser browser = new WebBrowser();
browser.DocumentText = Properties.Resources.<your_html_file>;

If you really need to have external .js files, I think you will probably need to make them embedded resources.如果您真的需要外部 .js 文件,我认为您可能需要将它们嵌入资源。 Then you can read these resources into a string of javascript.然后你可以将这些资源读入一串javascript。

string GetResourceString(string scriptFile) 
{ 
    Assembly assembly = Assembly.GetExecutingAssembly(); 
    Stream str = assembly.GetManifestResourceStream(scriptFile); 
    StreamReader sr = new StreamReader(str, Encoding.ASCII);
    return sr.ReadToEnd();
}

(Adapted from a reply on this page ) (改编自本页的回复

From here, look into IHTMLScriptElement.从这里开始,查看 IHTMLScriptElement。 From what I understand, you may be able to use this javascript string and set it as the ITHMLScriptElement's text field.据我了解,您可以使用此 javascript 字符串并将其设置为 ITHMLScriptElement 的文本字段。 See this question看到这个问题

Good luck.祝你好运。

Here's the file structure.这是文件结构。

在此处输入图片说明

I had success by doing this:我通过这样做取得了成功:

Set the properties of the html files in my solution like this:在我的解决方案中设置 html 文件的属性,如下所示:

Build Action -> Content
Copy to Output Directory -> Copy always

Configure my webBrowser object properties like this:像这样配置我的 webBrowser 对象属性:

var myAssembly = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
var path = myAssembly.Substring(0, myAssembly.Length - "MyLib.DLL".Length) + "WebViews/prototype/index.html";
webBrowser.Url = new Uri(path);

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

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