首页 > 移动平台 > 详细

#Windows Phone:在HTML5专案中,如何从Javascript传送字串到C#的APP端

时间:2014-03-09 02:08:14      阅读:548      评论:0      收藏:0      [点我收藏+]
原文:#Windows Phone:在HTML5专案中,如何从Javascript传送字串到C#的APP端

2013/11/29 02:50 | 閱讀數 : 505 我要推薦| Add Comment | 文章分類 : APPHTML5Windows Phone訂閱
bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

在開發Windows Phone APP的HTML5專案中,究竟該如何從Javascript傳給APP的C#程式碼中呢?現在就讓我來介紹一下吧!

使Javascript能與APP的C#程式碼溝通

首先先對嵌入至HTML5專案中的瀏覽器加上當觸發ScriptNotify事件時處理的函式,Javascript可藉此觸發ScriptNotify事件將字串傳給APP使用。在此範例裡,設定當Javascript傳字串過來時,就使用MessageBox.Show將字串顯示出來。

1
2
3
4
5
6
7
8
9
10
11
12
private void Browser_Loaded(object sender, RoutedEventArgs e)
{
  Browser.IsScriptEnabled = true;
  Browser.ScriptNotify += HTML_Script_Launched;
  // 在此加入您的 URL
  Browser.Navigate(new Uri(MainUri, UriKind.Relative));
}
 
private void HTML_Script_Launched(object sender, NotifyEventArgs e)
{
  MessageBox.Show(e.Value);
}

註:亦可直接從xaml中增加。<phone:WebBrowser x:Name="Browser" ScriptNotify="HTML_Script_Launched" />

接著只要在Javascript端,呼叫window.external.notify函式並傳值即可。

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="/html/css/phone.css" />
    <title>Windows Phone</title>
    <script>
      window.external.notify("Hello World!");
    </script>
  </head>
  <body>
    <div>
      <p>我的應用程式</p>
    </div>
    <div id="page-title">
      <p>頁面標題</p>
    </div>
  </body>
</html>

這樣就可以了!底下是結果:
bubuko.com,布布扣csjs03.png

參考資料

  1. Getting Started With Windows Phone 8 HTML5 Apps:http://blogs.msdn.com/b/matthiasshapiro/archive/2013/02/15/getting-started-with-windows-phone-8-html5-apps.aspx

DotBlogs Tags: APPHTML5Windows Phone

#Windows Phone:在HTML5专案中,如何从Javascript传送字串到C#的APP端,布布扣,bubuko.com

#Windows Phone:在HTML5专案中,如何从Javascript传送字串到C#的APP端

原文:http://www.cnblogs.com/lonelyxmas/p/3588627.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!