今天我终于实现了用webbrowser打开本地的一个html5的网页了,一路真是波折,主要的原因是因为,MS的所有默认值都不是我们所希望的,所以我们要去发现这些默认值,并修改他.
首先的问题是,如何打开本地的html,代码入下
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
if (false == isoStore.FileExists("HTMLPage1.htm"))
{
StreamResourceInfo sr = Application.GetResourceStream(new Uri("HTMLPage1.htm", UriKind.Relative));
using (BinaryReader br = new BinaryReader(sr.Stream))
{
byte[] data = br.ReadBytes((int)sr.Stream.Length);
using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile("HTMLPage1.htm")))
{
bw.Write(data);
bw.Close();
}
}
}
意思就是说,你要在IsolatedStorageFile(独立保存空间)中新建一个HTMLPage1.htm
然后你再能用
webBrowser1.Navigate(new Uri("HTMLPage1.htm", UriKind.Relative));
方法去打开这个页面.
这个html文件是放在项目里的,用content形式保存,我们需要在程序加载时,将资源文件转移到IsolatedStorageFile中,这里包括html所用到的所有本地文件
如图片,js,css等等
如果图片或js是用Resource形式保存的,那么我们这样读
StreamResourceInfo sr = Application.GetResourceStream(new Uri("/PhoneApp2;component/Background.png", UriKind.Relative));
PhoneApp2是项目文件名称
component是必写的
这样我们就可以在webbrowser里面打开html了
可惜,并不是html5的
我们还要在html文件里面加上这句话
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
这句话表示让用IE默认支持最高的版本作为文档格式,这样就支持html5了.
下面粘出更好的代码
private void SaveFilesToIsoStore()
{
//These files must match what is included in the application package,
//or BinaryStream.Dispose below will throw an exception.
string[] files = {
"CreateProduct.html"
};
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
if (false == isoStore.FileExists(files[0]))
{
foreach (string f in files)
{
StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
using (BinaryReader br = new BinaryReader(sr.Stream))
{
byte[] data = br.ReadBytes((int)sr.Stream.Length);
SaveToIsoStore(f, data);
}
}
}
}
private void SaveToIsoStore(string fileName, byte[] data)
{
string strBaseDir = string.Empty;
string delimStr = "/";
char[] delimiter = delimStr.ToCharArray();
string[] dirsPath = fileName.Split(delimiter);
//Get the IsoStore.
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
//Re-create the directory structure.
for (int i = 0; i < dirsPath.Length - 1; i++)
{
strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
isoStore.CreateDirectory(strBaseDir);
}
//Remove the existing file.
if (isoStore.FileExists(fileName))
{
isoStore.DeleteFile(fileName);
}
//Write the file.
using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
{
bw.Write(data);
bw.Close();
}
}
更多信息请查看 java进阶网 http://www.javady.com/index.php/category/thread
分享到:
相关推荐
webbrowser+Nsoup 实现百度文库内容复制 NSoup.Nodes.Document NSoupDoc = NSoup.NSoupClient.Parse(webBrowser1.Document.Body.InnerHtml); var strpagecount = NSoupDoc.Body.Select(".page-count").Text; if ...
WPF+Webbrowser+Echarts+Naduio音频转换像素点位,像素点位转时间x与y关系点位,实现动态时间波形图,时间精确到毫秒。附原始MP3文件转wav文件,项目可直接运行
DELPHI+chrome+替换WebBrowser让你的程序嵌入chrome浏览器,此控件非常方便的让你的程序内置chrome浏览器,解决老webbrowser的各种问题,非常方便。Chrome不挑食,基本上什么系统都可以跑,而且速度那是飞快,所以C/...
实现了Webbrowser访问网页,可以点击网页中的Flash元素,例如网页游戏等等 实现了Webbrowser访问网页,可以点击网页中的Flash元素,例如网页游戏等等
WPF WebBrowser嵌入html,并与js实现互操作
vb系统托盘+Webbrowser的应用
Delphi WebBrowser 将一段HTML文字显示在网页浏览器中,实际是创建了一个HTML格式内容并显示出来,主要使用Delphi中内置的WebBrowser方法来实现: WebBrowser1.Navigate('about:blank'); while(tag=0) do ...
MFC WebBrowser控件加载HTMLVC++源码,WebBrowser控件加载HTML网页时不显示滚动条,实现事件 documentcomplete,当HTML下载完毕时再显示出来,然后在此事件代码中去掉滚动条。.zip
浏览器开发适用于各种电脑,是通过VS2005开发的一款适合个人的浏览器
VS2010 C++Windows 窗体应用程序 WebBrowser 链接网页
使用C# 动态生成Html并在WebBroser中显示,比较简单易懂,适合初学者
webBrowser控件操作动态html,源码都有,点击下载
采用WebBrowser伪装成浏览器访问,绕过了反爬虫程序,获取一个目标网页的数据
只要把应用程序属性类的 "com可见"选上. 再给 this.webBrowser1.ObjectForScripting = this; 赋值就行了 InvokeScript方式: 另外:InvokeScript 还可以带参数的形式执自定义行脚本方法 例如: webBrowser1.Document....
在WinForm程序中,webBrowser显示页面使用谷歌内核。避免了网页在WinForm中显示兼容性问题。
webbrowser打造超酷图形界面,很多大型软件在用的界面交互技术
关于Microsoft Web浏览器的应用
WebBrowser显示html,显示相应的html路径的文档
MFCApplicationWeb_WebBrowser的应用_加了与JavaScript交互 MFCApplicationWeb_WebBrowser的应用_加了与JavaScript交互 MFCApplicationWeb_WebBrowser的应用_加了与JavaScript交互