使用HTML5制作Window Phone 7程序

首先的问题是,如何打开本地的html,代码入下

IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
             if (false == isoStore.FileExists(“HTMLPage1.htm“))
             {
                 StreamResourceInfo sr = Application.GetResourceStream(newUri(“HTMLPage1.htm“, UriKind.Relative));
                 using (BinaryReader br = new BinaryReader(sr.Stream))
                 { 
                     byte[] data = br.ReadBytes((int)sr.Stream.Length);
                     using (BinaryWriter bw = newBinaryWriter(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(“/WPApp;component/Background.png”, UriKind.Relative));

WPApp是项目文件名称、component是必写的,这样我们就可以在webbrowser里面打开html了。可惜,并不是html5的,我们还要在html文件里面加上这句话

这句话表示让用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);
            }

  &
nbsp;         //Write the file.            using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
            {
                bw.Write(data);
                bw.Close();
            }
        }

来源:houoop