当前位置: 代码迷 >> Web前端 >> wp7 webbrowser+html5 制造本地应用程序(一)
  详细解决方案

wp7 webbrowser+html5 制造本地应用程序(一)

热度:879   发布时间:2012-08-16 12:02:15.0
wp7 webbrowser+html5 打造本地应用程序(一)

?

今天我终于实现了用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