Esempio n. 1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.webflow_mywebview);

            #region WebView

            var client = new ContentWebViewClient();

            client.WebViewLocaitonChanged += (object sender, ContentWebViewClient.WebViewLocaitonChangedEventArgs e) => {
                Debug.WriteLine(e.CommandString);
            };

            client.WebViewLoadCompleted += (object sender, ContentWebViewClient.WebViewLoadCompletedEventArgs e) => {
                RunOnUiThread(() => {
                    //AndHUD.Shared.Dismiss(this);
                    Debug.WriteLine("WebViewLoad");
                });
            };


            MyWebView = FindViewById <WebView>(Resource.Id.webflow_webview_viewweb);
            // NOTICE : 先換成一般的 WebViewClient
            //MyWebView.SetWebViewClient(client);
            MyWebView.SetWebViewClient(new MyWebClient());

            MyWebView.Settings.JavaScriptEnabled = true;
            MyWebView.Settings.UserAgentString   = @"Android";

            // 負責與頁面溝通 - WebView -> Native
            MyJSInterface myJSInterface = new MyJSInterface(this);

            MyWebView.AddJavascriptInterface(myJSInterface, "TP");
            myJSInterface.CallFromPageReceived += delegate(object sender, MyJSInterface.CallFromPageReceivedEventArgs e) {
                //MyWebView.LoadUrl("http://developer.xamarin.com");

                Debug.WriteLine(e.Result);
            };

            // 負責與頁面溝通 - Native -> WebView
            JavaScriptResult callResult = new JavaScriptResult();
            callResult.JavaScriptResultReceived += (object sender, JavaScriptResult.JavaScriptResultReceivedEventArgs e) => {
                Debug.WriteLine(e.Result);
            };


            // 載入一般網頁
            //MyWebView.LoadUrl ("http://stackoverflow.com/");
            // 載入以下程式碼進行互動

            MyWebView.LoadDataWithBaseURL(
                null
                , @"<html>
						<head>
							<title>Local String</title>
							<style type='text/css'>p{font-family : Verdana; color : purple }</style>
							<script language='JavaScript'> 
								var lookup = '中文訊息'
								function msg(){ window.location = 'callfrompage://Hi'  }
							</script>
						</head>
						<body><p>Hello World!</p><br />
							<button type='button' onclick='TP.CallFromPage(lookup)' text='Hi From Page'>Hi From Page</button>
						</body>
					</html>"
                , "text/html"
                , "utf-8"
                , null);



            #endregion

            BtnGo        = FindViewById <Button>(Resource.Id.webflow_webview_btngo);
            BtnGo.Click += (object sender, EventArgs e) => {
                RunOnUiThread(() => {
                    MyWebView.EvaluateJavascript(@"msg();", callResult);
                });

                /*
                 *              var url = TxtUrl.Text.Trim() ;
                 *
                 *              AlertDialog.Builder alert = new AlertDialog.Builder (this);
                 *              alert.SetTitle (url);
                 *              alert.SetNegativeButton( "取消", (senderAlert, args) =>{
                 *
                 *
                 *              });
                 *              alert.SetPositiveButton( "確認", (senderAlert, args) =>{
                 *
                 *                      RunOnUiThread(
                 *                              ()=>{
                 *                                      AndHUD.Shared.Show(this, "Status Message", -1, MaskType.Clear);
                 *                              }
                 *
                 *                      );
                 *
                 *                      MyWebView.LoadUrl (url);
                 *
                 *              });
                 *
                 *              RunOnUiThread (() => {
                 *                      alert.Show();
                 *              } );
                 *
                 *              //
                 *              _InputMethodManager.HideSoftInputFromWindow(
                 *                      TxtUrl.WindowToken,
                 *                      HideSoftInputFlags.None );
                 */
            };
        }