如何在WP7的WebBrowser控件中读取现有的html文件?
如何在WP7的WebBrowser控件中读取现有的html文件?
我尝试过:
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("abc.htm"); StreamReader 阅读器 = new StreamReader(stream); 字符串 html = reader.ReadToEnd(); Browser.NavigateToString(html);
Browser.Navigate(new Uri("abc.htm", UriKind.Relative));
var rs = Application.GetResourceStream(new Uri("abc.htm", UriKind.Relative)); StreamReader sr = new StreamReader(rs.Stream); 字符串 html = sr.ReadToEnd(); Browser.NavigateToString(html);
这三个都不起作用。方法1和3给出了NullReferenceException,因为流返回null,基本上它没有读取我的html文件。
这里有什么问题呢?我该如何修复它?
How to read an existing html file in the WebBrowser control in WP7?
I have tried:
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("abc.htm");
StreamReader reader = new StreamReader(stream);
string html = reader.ReadToEnd();
Browser.NavigateToString(html);Browser.Navigate(new Uri("abc.htm", UriKind.Relative));
var rs = Application.GetResourceStream(new Uri("abc.htm", UriKind.Relative));
StreamReader sr = new StreamReader(rs.Stream);
string html = sr.ReadToEnd();
Browser.NavigateToString(html);
All three are not working. The methods 1 and 3 gives NullReferenceException because the stream returns null, basically it is not reading my html file.
What is the problem here? How can i fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您使用数字 3 时,请确保您的 HTML 文件的构建类型设置为“内容”。
When you use number 3, make sure your HTML file has its build type set to 'Content'.
如果您想使用选项 2,则必须首先将文件复制到独立存储(如果需要引用任何其他文件),因为在文档中无法使用相对路径(到其他文档、图像、js、css 等),如果直接从 XAP 加载。
If you want to use option 2 you must first copy the file to isolated storage if it needs to reference any other files as it's not possible to use relative paths (to other docs, images, js, css, etc.) within a document if loaded directly from the XAP.
好的,这是一个示例:
在 MainPage.xaml 中:
在 MainPage.xaml.cs 中,
确保您有文件“abc.html”(检查拼写错误,您的示例代码是“abc.htm” )在项目的根文件夹中。将操作构建为“内容”,将“复制到输出目录”构建为“不复制”。它应该在浏览器中显示该页面。
Ok, so here's a sample:
in MainPage.xaml:
in MainPage.xaml.cs
make sure you have the file "abc.html" (check for typo, your sample code is "abc.htm") in your project's root folder. Build action as 'Content' and 'copy to output directory' as 'Do not copy'. It should display the page in the browser.
对于第一和第三:
将 html 文件的“构建操作属性”设置为“资源”
否则它会给你 NullReferenceException
对于第二个:
WP7 中的浏览器控件无法直接导航到项目中添加的 html 文件。为此,html 文件必须位于独立存储中。
尝试一下,我希望它会起作用。
(标记答案,如果有帮助的话)。
For the first and third :
set the Build Action Property of the html file to "Resource"
otherwise it will give you NullReferenceException
For the second :
browser control in WP7 cannot navigate directly to a html file added in the project. To do this the html file must be located in Isolated Storage.
Try that, i hope it'll work.
(Mark answer, if its helpful).
如果您将命令添加到按钮内,则效果很好,例如:
It works fine if you add the command inside a button like: