C # function call from Javascript in Xamarin Forms PCL

I am developing a cross-platform application in C # using Xamarin Forms. I am new to xamarin. I created a portable cross-platform application. In which I try to open local html pages in webview. To call the C # function from javascript, I implemented a hybrid webview using XAMARIN FORUM .

<ContentPage.Content>
    <local:HybridWebView x:Name="hybridWebView"  Uri="index.html" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
  </ContentPage.Content>

I call invokeCSharpAction()which is in index.html.

In the Droid project

 public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
    {
        const string JavaScriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";

        protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
        {
            base.OnElementChanged(e);
            Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);

            if (Control == null)
            {
                var webView = new Android.Webkit.WebView(Forms.Context);
                webView.Settings.JavaScriptEnabled = true;
                webView.Settings.DomStorageEnabled = true;
                Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);

                webView.SetWebChromeClient(new WebChromeClient());
                webView.SetWebViewClient(new WebViewClient());
                webView.AddJavascriptInterface(new JSBridge(this), "jsBridge");
                SetNativeControl(webView);
            }
            if (e.OldElement != null)
            {
                Control.RemoveJavascriptInterface("jsBridge");
                var hybridWebView = e.OldElement as HybridWebView;
                hybridWebView.Cleanup();
            }
            if (e.NewElement != null)
            {
                Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
                Control.LoadUrl(string.Format("file:///android_asset/Content/{0}", Element.Uri));
                InjectJS(JavaScriptFunction);
            }
        }

        void InjectJS(string script)
        {
            if (Control != null)
            {
                Control.LoadUrl(string.Format("javascript: {0}", script));
            }
        }

    }

JSBridge.cs

public class JSBridge : Java.Lang.Object
    {
        readonly WeakReference<HybridWebViewRenderer> hybridWebViewRenderer;

        public JSBridge(HybridWebViewRenderer hybridRenderer)
        {
            hybridWebViewRenderer = new WeakReference<HybridWebViewRenderer>(hybridRenderer);
        }

        [JavascriptInterface]
        [Export("invokeAction")]
        public void InvokeAction(String data)
        {

            HybridWebViewRenderer hybridRenderer;

            if (hybridWebViewRenderer != null && hybridWebViewRenderer.TryGetTarget(out hybridRenderer))
            {
                hybridRenderer.Element.InvokeAction(data);
            }
        }

        [JavascriptInterface]
        [Export("invokeDownloadAction")]
        public void invokeDownloadAction(String data)
        {

            HybridWebViewRenderer hybridRenderer;

            if (hybridWebViewRenderer != null && hybridWebViewRenderer.TryGetTarget(out hybridRenderer))
            {
              //  hybridRenderer.Element.InvokeAction(data);
                hybridRenderer.Element.InvokeAction(data);
            }
        }
    }

Suppose I went to home.html using window.location in index.html Now, if I try to call the same invokeCSharpAction(), the VS output window says

invokeCSharpAction not defined

How can I handle this situation. Can I have an OnNavigated and OnNavigating Event for my hybrid web view?

+4

Source: https://habr.com/ru/post/1661833/


All Articles