Exemple #1
0
        public void LoadHTML(string html)
        {
            wb.LoadHtmlString(html, "about:blank");
            // Probably should make this conditional.
            // We use a timeout so we don't sit here forever if a document fails to load.

            Stopwatch watch = new Stopwatch();

            watch.Start();
            while (wb != null && wb.LoadStatus != LoadStatus.Finished && watch.ElapsedMilliseconds < 10000)
            {
                while (Gtk.Application.EventsPending())
                {
                    Gtk.Application.RunIteration();
                }
            }
        }
Exemple #2
0
    private void CreateBrowser()
    {
        //Создаем массив обработчиков доступных для вызова из js
        handlers = new Dictionary<string, Action<NameValueCollection>>
        {
            { "callfromjs", nv => CallJs("showMessage", new object[] { nv["msg"] + " Ответ из С#" }) }
        };

        browser = new WebView ();

        browser.NavigationRequested += (sender, args) =>
        {
            var url = new Uri(args.Request.Uri);
            if (url.Scheme != "mp")
            {
                //mp - myprotocol.
                //Обрабатываем вызовы только нашего специального протокола.
                //Переходы по обычным ссылкам работают как и прежде
                return;
            }

            var parameters = System.Web.HttpUtility.ParseQueryString(url.Query);

            handlers[url.Host.ToLower()](parameters);

            //Отменяем переход по ссылке
            browser.StopLoading();
        };

        browser.LoadHtmlString (@"
                <html>
                    <head></head>
                    <body id=body>
                        <h1>Интерфейс</h1>
                        <button id=btn>Вызвать C#</button>
                        <p id=msg></p>

                        <script>
                            function buttonClick() {
                                window.location.href = 'mp://callFromJs?msg=Сообщение из js.';
                            }
                            function showMessage(msg) {
                                document.getElementById('msg').innerHTML = msg;
                            }

                            document.getElementById('btn').onclick = buttonClick;
                        </script>
                    </body>
                </html>
            ", null);

        this.Add (browser);
    }