using System;
using System.Windows;
using Microsoft.Phone.Controls;

namespace WPWebBrowserInteraction
{
    public partial class MainPage : PhoneApplicationPage
    {
        public const string HTML =
@"<html>
<head>
    <title>InnerHTMLPage</title>
    <script type='text/javascript'>
        function DataReceivedFromPhoneApp(input) {
            textReceived.innerHTML = input;
            return true;
        }

        function SendDataToPhoneApp() {
            window.external.Notify(textToSend.value);
        }
    </script>
</head>
<body>
    <div>
        Zu sendenden Wert eingeben:<br>
        <input name='textToSend' type='text' value='An Anwendung senden...' />
    </div>
    <div>
        <input type='button' value='An Anwendung senden' onClick='SendDataToPhoneApp()'/>
        <br />
        <br />
    </div>
    <div>Wert von Anwendung erhalten:</div>
    <div id='textReceived' style='font-weight: bold; color: #800000; border: thin solid #000000'></div>
</body>
</html>";

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            webBrowser1.NavigateToString(HTML);

        }

        private void webBrowser1_ScriptNotify(object sender, NotifyEventArgs e)
        {
            MessageBox.Show("Wert von HTML erhalten:\r\n\r\n" + e.Value.ToString());
        }

        private void btnSendToBrowser_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                webBrowser1.InvokeScript("DataReceivedFromPhoneApp", txtToBrowser.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
