Ejemplo n.º 1
0
        private void SubscribeEvent(SubscribeEventData data)
        {
            EventRegistration eventRegistration = serviceDispatcher.SubscribeEvent(data.ServiceName, data.EventName, this, (s, e) =>
            {
                if (data.CallbackId != null)
                {
                    Application.Current.Dispatcher.Invoke(() => webBrowser.InvokeScript("eval", string.Format("serviceDispatcher._eventCallback('{0}', '{1}')", data.CallbackId, JsonConvert.SerializeObject(e))));
                }
            });

            eventRegistrations.AddOrUpdate(data.CallbackId, eventRegistration, (k, e) => e);
        }
Ejemplo n.º 2
0
        public WebBrowserPage(IServiceDispatcher serviceDispatcher, IPlatformProvider platformProvider)
        {
            webView            = new ShellWebView();
            eventRegistrations = new ConcurrentDictionary <string, EventRegistration>();

            webView.RegisterCallback("initialize", args =>
            {
                platformProvider.ExecuteOnUIThread(() => webView.InjectJavaScript(string.Format("serviceDispatcher._initializeCallback({0});", JsonConvert.SerializeObject(serviceDispatcher.Services))));
            });

            webView.RegisterCallback("dispatch", args =>
            {
                DispatchData dispatchData = JsonConvert.DeserializeObject <DispatchData>(args);

                string result = string.IsNullOrEmpty(dispatchData.InstanceName)
                    ? JsonConvert.SerializeObject(serviceDispatcher.Dispatch(dispatchData.ServiceName, dispatchData.MethodName, dispatchData.Arguments))
                    : JsonConvert.SerializeObject(serviceDispatcher.Dispatch(dispatchData.ServiceName, dispatchData.InstanceName, dispatchData.MethodName, dispatchData.Arguments));

                if (dispatchData.CallbackId != null)
                {
                    platformProvider.ExecuteOnUIThread(() => webView.InjectJavaScript(string.Format("serviceDispatcher._dispatchCallback('{0}', {1});", dispatchData.CallbackId, result)));
                }
            });

            webView.RegisterCallback("subscribeEvent", args =>
            {
                SubscribeEventData subscribeEventData = JsonConvert.DeserializeObject <SubscribeEventData>(args);

                EventRegistration eventRegistration = serviceDispatcher.SubscribeEvent(subscribeEventData.ServiceName, subscribeEventData.EventName, this, (s, e) =>
                {
                    if (subscribeEventData.CallbackId != null)
                    {
                        platformProvider.ExecuteOnUIThread(() => webView.InjectJavaScript(string.Format("serviceDispatcher._eventCallback('{0}', '{1}');", subscribeEventData.CallbackId, JsonConvert.SerializeObject(e))));
                    }
                });

                eventRegistrations.AddOrUpdate(subscribeEventData.CallbackId, eventRegistration, (k, e) => e);
            });

            webView.RegisterCallback("unsubscribeEvent", args =>
            {
                UnsubscribeEventData unsubscribeEventData = JsonConvert.DeserializeObject <UnsubscribeEventData>(args);

                EventRegistration eventRegistration = null;
                if (eventRegistrations.TryRemove(unsubscribeEventData.CallbackId, out eventRegistration))
                {
                    serviceDispatcher.UnsubscribeEvent(unsubscribeEventData.ServiceName, eventRegistration);
                }
            });

            Content = webView;

            SetBinding(UrlProperty, new Binding("Url"));
            SetBinding(HtmlProperty, new Binding("Html"));

            webView.NavigationCompleted += (s, e) =>
            {
                if (webView.Source is UrlWebViewSource)
                {
                    Url = e;
                }
            };
        }