MainPage.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. using System.Net.Browser;
  17. using System.Windows.Navigation;
  18. namespace WhereAmI
  19. {
  20. // Nokia Maps API
  21. // api.maps.nokia.com/
  22. // Bing Maps API
  23. // http://www.microsoft.com/maps/
  24. // http://msdn.microsoft.com/en-us/library/gg427610.aspx
  25. public partial class MainPage : PhoneApplicationPage
  26. {
  27. private BrowserMouseHelper moveHelper;
  28. // Constructor
  29. public MainPage()
  30. {
  31. InitializeComponent();
  32. webBrowser.Opacity = 0;
  33. // Enable Geolocation and javascript for the browser
  34. webBrowser.IsGeolocationEnabled = true;
  35. webBrowser.IsScriptEnabled = true;
  36. moveHelper = new BrowserMouseHelper(ref webBrowser);
  37. moveHelper.ScrollDisabled = true;
  38. // Store all HTML, Javascript, CSS and image files into isolated storage and use them for there
  39. SaveFilesToIsoStore();
  40. // Listening javascript notifications from HTML
  41. webBrowser.ScriptNotify += new EventHandler<NotifyEventArgs>(webBrowser_ScriptNotify);
  42. }
  43. /// <summary>
  44. /// Open index.html page when this Silverlight page is opened
  45. /// </summary>
  46. /// <param name="e"></param>
  47. protected override void OnNavigatedTo(NavigationEventArgs e)
  48. {
  49. base.OnNavigatedTo(e);
  50. webBrowser.Navigate(new Uri("html_content/index.html", UriKind.Relative));
  51. }
  52. /// <summary>
  53. /// Close info html page if user press device Back key there
  54. /// </summary>
  55. /// <param name="e"></param>
  56. protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
  57. {
  58. base.OnBackKeyPress(e);
  59. if (isInfoVisible())
  60. {
  61. e.Cancel = true;
  62. CloseInfo();
  63. }
  64. else
  65. {
  66. e.Cancel = false;
  67. }
  68. }
  69. /// <summary>
  70. /// Javascript calls C#. Show message to the user as MessageBox
  71. /// </summary>
  72. /// <param name="sender"></param>
  73. /// <param name="e"></param>
  74. void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
  75. {
  76. MessageBox.Show(e.Value);
  77. }
  78. /// <summary>
  79. /// Html page loaded, show webbrowser
  80. /// </summary>
  81. /// <param name="sender"></param>
  82. /// <param name="e"></param>
  83. private void webBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
  84. {
  85. logo.Opacity = 0;
  86. // Show browser
  87. webBrowser.Opacity = 1;
  88. }
  89. /// <summary>
  90. /// Store all HTML, Javascript, CSS and image files into isolated storage and use them for there
  91. /// </summary>
  92. private void SaveFilesToIsoStore()
  93. {
  94. //These files must match what is included in the application package,
  95. //or BinaryStream.Dispose below will throw an exception.
  96. string[] files = {
  97. "html_content/index.html",
  98. "html_content/css/whereami.css",
  99. "html_content/images/marker.png",
  100. "html_content/js/geolocation.js",
  101. "html_content/js/staticmap.js"
  102. };
  103. IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
  104. // For testing
  105. isoStore.Remove();
  106. foreach (string f in files)
  107. {
  108. StreamResourceInfo sr = Application.GetResourceStream(new Uri(f, UriKind.Relative));
  109. using (BinaryReader br = new BinaryReader(sr.Stream))
  110. {
  111. byte[] data = br.ReadBytes((int)sr.Stream.Length);
  112. SaveToIsoStore(f, data);
  113. }
  114. }
  115. }
  116. private void SaveToIsoStore(string fileName, byte[] data)
  117. {
  118. string strBaseDir = string.Empty;
  119. string delimStr = "/";
  120. char[] delimiter = delimStr.ToCharArray();
  121. string[] dirsPath = fileName.Split(delimiter);
  122. //Get the IsoStore.
  123. IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
  124. //Re-create the directory structure.
  125. for (int i = 0; i < dirsPath.Length - 1; i++)
  126. {
  127. strBaseDir = System.IO.Path.Combine(strBaseDir, dirsPath[i]);
  128. isoStore.CreateDirectory(strBaseDir);
  129. }
  130. //Remove the existing file.
  131. if (isoStore.FileExists(fileName))
  132. {
  133. isoStore.DeleteFile(fileName);
  134. }
  135. //Write the file.
  136. using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
  137. {
  138. bw.Write(data);
  139. bw.Close();
  140. }
  141. }
  142. /// <summary>
  143. /// Call javascript to update map
  144. /// </summary>
  145. private void UpdateMap()
  146. {
  147. try
  148. {
  149. if (isInfoVisible() == false)
  150. webBrowser.InvokeScript("update");
  151. }
  152. catch { }
  153. }
  154. /// <summary>
  155. /// Ask from javascript is info page visible
  156. /// </summary>
  157. /// <returns></returns>
  158. private bool isInfoVisible()
  159. {
  160. try
  161. {
  162. int ret = Int32.Parse((string)(webBrowser.InvokeScript("isInfoVisible")));
  163. if (ret == 0)
  164. return false;
  165. else
  166. return true;
  167. }
  168. catch
  169. {
  170. return false;
  171. }
  172. }
  173. /// <summary>
  174. /// Ask javascript to open info page
  175. /// </summary>
  176. private void ShowInfo()
  177. {
  178. try
  179. {
  180. webBrowser.InvokeScript("showInfo");
  181. }
  182. catch { }
  183. }
  184. /// <summary>
  185. /// Ask javascript to close info
  186. /// </summary>
  187. private void CloseInfo()
  188. {
  189. try
  190. {
  191. webBrowser.InvokeScript("closeInfo");
  192. }
  193. catch { }
  194. }
  195. private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
  196. {
  197. ShowInfo();
  198. }
  199. private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e)
  200. {
  201. UpdateMap();
  202. }
  203. private void ApplicationBarIconButton_Click(object sender, EventArgs e)
  204. {
  205. UpdateMap();
  206. }
  207. private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
  208. {
  209. ShowInfo();
  210. }
  211. }
  212. }