Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

HybridWebView not work properly

$
0
0

Hey,

I have a HybridWebView like the MS example.

But my website is made with ASP Core, and I can't call C# Action from Javascript in my website.

So I ask if someone as already done this. And something which is weird is I did reverse I implement call javascript from C# and it works so I don't understand very well.

The code :

My custom render :

const string JavascriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";
if (Control == null)
            {
                // Instantiate the native control and assign it to the Control property with
                // the SetNativeControl method

                var webView = new Android.Webkit.WebView(_context);
                webView.Settings.JavaScriptEnabled = true;

                // Implement WebViewClient who receive notification and request in JS with personalize function => JavascriptFunction
                //webView.SetWebViewClient(new JavascriptWebViewClient($"javascript: {JavascriptFunction}"));
                webView.SetWebViewClient(new JavascriptWebViewClient(JavascriptFunction));
                webView.SetWebChromeClient(new JavascriptWebChromeClient());

                SetNativeControl(webView);
            }
            if (e.OldElement != null)
            {
                // Unsubscribe from event handlers and cleanup any resources
                Control.RemoveJavascriptInterface("jsBridge");
                var hybridWebView = e.OldElement as HybridWebView;
                //var context = hybridWebView.BindingContext as ViewModelBase;

                // Clean old action to execute in JS
                //context.CleanAllActionInJS();
                hybridWebView.Cleanup();
            }
            if (e.NewElement != null)
            {
                // Configure the control and subscribe to event handlers
                Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
                var view = e.NewElement as HybridWebView;

                //Control.LoadUrl($"file:///android_asset/Content/{Element.Url}");
                // Add Url to hybridWebview
                Control.LoadUrl(Element.Url);
            }


    public class JavascriptWebViewClient : WebViewClient
        {
            private string _javascript;

            public JavascriptWebViewClient(string javascript)
            {
                _javascript = javascript;
            }

            /// <summary>
            /// When page is charged, execute JS
            /// </summary>
            /// <param name="view"></param>
            /// <param name="url"></param>
            public override void OnPageFinished(WebView view, string url)
            {
                base.OnPageFinished(view, url);
                view.EvaluateJavascript(_javascript, null);

            }
    }


 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))
            {
                //var context = hybridRenderer.Element.BindingContext as ViewModelBase;

                // Depending if pass or not data
                if (!string.IsNullOrEmpty(data)) hybridRenderer.Element.InvokeAction(data);
                else hybridRenderer.Element.InvokeAction();
            }
        }
}

So when I use this in my website the way I think is to do : invokeCSharpAction(mydata) in a JS function.

Thanks


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>