如何从嵌入的资源加载HTML / JavaScript插入WinForm的Web浏览器 [英] How to load HTML/JavaScript from embedded resource into winform web browser
问题描述
我想有使用JavaScript的一些HTML文件加载到一个WinForms(V2.0)应用程序的Web浏览器控件。在执行过程中,我不会上网,所以JavaScript和HTML表单将被嵌入在他resources.resx文件。
I want to have some HTML files with JavaScript loaded into the web browser control in a winforms (v2.0) application. During execution, I won't have internet access, so JavaScript and HTML forms will be embedded in he resources.resx file.
1)我怎么可以加载一个HTML文档出资源(类似于一个文件:///操作,但是它没有被从文件系统加载),
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),
2)如何将席宣布JavaScript脚本的加载? 。即,
2) how would I declare the JavaScript scripts to be loaded? I.e.,
<script src=resource.jquery.min.js??? ... />
谢谢!
Thanks!
推荐答案
要加载HTML文档,只需编译你的HTML文件作为嵌入资源,然后:
To load the HTML document, just compile your html file as embedded resource, and then:
WebBrowser browser = new WebBrowser();
browser.DocumentText = Properties.Resources.<your_html_file>;
如果你真的需要有外部的.js文件,我想你可能会需要让他们嵌入资源。然后,你可以阅读这些资源为JavaScript字符串。
If you really need to have external .js files, I think you will probably need to make them embedded resources. Then you can read these resources into a string of javascript.
string GetResourceString(string scriptFile)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream str = assembly.GetManifestResourceStream(scriptFile);
StreamReader sr = new StreamReader(str, Encoding.ASCII));
return sr.ReadToEnd();
}
(从的这个页面回复)
从这里,看看IHTMLScriptElement。据我了解,你可以使用这个JavaScript字符串,并将其设置为ITHMLScriptElement的文本字段。请参见
From here, look into IHTMLScriptElement. From what I understand, you may be able to use this javascript string and set it as the ITHMLScriptElement's text field. See this question
祝你好运。
这篇关于如何从嵌入的资源加载HTML / JavaScript插入WinForm的Web浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!