您的位置:首页 > 移动开发

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

2014-03-08 21:21 435 查看
原文:#Windows Phone:在HTML5专案中,如何从Javascript传送字串到C#的APP端


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

2013/11/29 02:50 |
閱讀數 : 505
|

我要推薦

|
Add Comment

| 文章分類 :

APP

HTML5

Windows Phone

|
訂閱











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


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

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

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>


這樣就可以了!底下是結果:



csjs03.png


參考資料

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

var now = new Date();var index=now.getTime();document.write('');



DotBlogs Tags:

APP

HTML5

Windows Phone

關連文章

#Windows Phone:安裝Windows Phone SDK 8.0流程

#Monogame:在Windows 8 APP專案中,判斷Snap View的狀態

#Windows Phone:在HTML5專案中,如何從C#執行網頁的Javascript程式

#APP:[龜兔賽跑棋]於Windows Store上架
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: