MainPage.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13. using System.IO;
  14. using System.Windows.Resources;
  15. using System.IO.IsolatedStorage;
  16. namespace HTML5App
  17. {
  18. public partial class MainPage : PhoneApplicationPage
  19. {
  20. // HTML5 and CSS3 on Windows Phone: Dark and Light Styles
  21. // http://robtiffany.com/windows-phone/html5-and-css3-on-windows-phone-dark-and-light-styles
  22. // zooming and scrolling
  23. // http://www.scottlogic.co.uk/blog/colin/2011/11/suppressing-zoom-and-scroll-interactions-in-the-windows-phone-7-browser-control/#comment-96355
  24. public MainPage()
  25. {
  26. InitializeComponent();
  27. webBrowser.Opacity = 0;
  28. btn1.Opacity = 0;
  29. // TODO: does not work?
  30. //SetBrowserBackground();
  31. // Store all HTML, Javascript, CSS and image files into isolated storage and use them for there
  32. // http://msdn.microsoft.com/en-us/library/ff431811%28v=vs.92%29.aspx
  33. SaveFilesToIsoStore();
  34. //webBrowser.Navigate(new Uri("html_content/metroIndex.html", UriKind.Relative));
  35. webBrowser.Navigate(new Uri("html_content/jqueryIndex.html", UriKind.Relative));
  36. // Listening javascript notifications from HTML
  37. webBrowser.ScriptNotify += new EventHandler<NotifyEventArgs>(webBrowser_ScriptNotify);
  38. }
  39. // Change brower background to same than application
  40. private void SetBrowserBackground()
  41. {
  42. Color mc =
  43. (Color)Application.Current.Resources["PhoneBackgroundColor"];
  44. webBrowser.Background = new SolidColorBrush(mc);
  45. }
  46. void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
  47. {
  48. MessageBox.Show(e.Value);
  49. }
  50. private void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
  51. {
  52. // Show browser
  53. webBrowser.Opacity = 1;
  54. btn1.Opacity = 1;
  55. }
  56. private void Button_Click(object sender, RoutedEventArgs e)
  57. {
  58. // Run javascript from C#
  59. webBrowser.InvokeScript("someFunction", new String[]{"C#"});
  60. }
  61. private void SaveFilesToIsoStore()
  62. {
  63. //These files must match what is included in the application package,
  64. //or BinaryStream.Dispose below will throw an exception.
  65. string[] files = {
  66. "html_content/metroIndex.html",
  67. "html_content/jqueryIndex.html",
  68. "html_content/script.js",
  69. "html_content/MetroDark.css",
  70. "html_content/logo.png",
  71. "html_content/jquery.mobile-1.0.1.css",
  72. "html_content/jquery.mobile.structure-1.0.1.css",
  73. "html_content/jquery-1.6.4.js",
  74. "html_content/jquery.mobile-1.0.1.js"
  75. };
  76. IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
  77. //if (false == isoStore.FileExists(files[0]))
  78. //{
  79. foreach (string f in files)
  80. {
  81. StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
  82. using (BinaryReader br = new BinaryReader(sr.Stream))
  83. {
  84. byte[] data = br.ReadBytes((int)sr.Stream.Length);
  85. SaveToIsoStore(f, data);
  86. }
  87. }
  88. //}
  89. }
  90. private void SaveToIsoStore(string fileName, byte[] data)
  91. {
  92. string strBaseDir = string.Empty;
  93. string delimStr = "/";
  94. char[] delimiter = delimStr.ToCharArray();
  95. string[] dirsPath = fileName.Split(delimiter);
  96. //Get the IsoStore.
  97. IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
  98. //Re-create the directory structure.
  99. for (int i = 0; i < dirsPath.Length - 1; i++)
  100. {
  101. strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
  102. isoStore.CreateDirectory(strBaseDir);
  103. }
  104. //Remove the existing file.
  105. if (isoStore.FileExists(fileName))
  106. {
  107. isoStore.DeleteFile(fileName);
  108. }
  109. //Write the file.
  110. using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
  111. {
  112. bw.Write(data);
  113. bw.Close();
  114. }
  115. }
  116. }
  117. }