HybridWebView not work properly

theocangtheocang Member ✭✭✭

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

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Your code looks no problem , It should be able to invoke c# form Javascript :https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview#invoking-c-from-javascript

    What do you mean by I did reverse I implement call javascript from C# ?

  • theocangtheocang Member ✭✭✭

    @ColeX
    Hey, I mean I would invoke C# from Javascript that don't work, but I implement reverse so I can call JS from C#.
    I implement the tuto of Xamarin Help for that : https://xamarinhelp.com/xamarin-forms-webview-executing-javascript/

  • ColeXColeX Member, Xamarin Team Xamurai
  • theocangtheocang Member ✭✭✭

    @ColeX Yes I already say in my first message.

  • ColeXColeX Member, Xamarin Team Xamurai

    I test MS sample on my side , it works perfect.

    Could you provide your html file to me for testing ?

  • theocangtheocang Member ✭✭✭

    @ColeX I can't but my html file in okay, I think it's because there is a issue when the JS is injected into html page, that the page isn't in local, it's is hosted with the website in ASP Core. I have try to call with button inline and in the Javascript and no ways works. Also when I call the JS event from C# it's working. So I am don't know what to do.

  • ColeXColeX Member, Xamarin Team Xamurai

    Do you mean that the remote website display well in HybridWebView , however it did not call c# method when you click button in it ?

    okay, the client code looks no problem , i think you should double check your web side , etc, whether the button could trigger the function correctly , whether the method name inside click event match to c# method .

  • theocangtheocang Member ✭✭✭

    @ColeX yes, exacly, you've right that what I think too, however I think the trigger is ok, I try to do in JS file but as the "function invokeCSharpAction(data){jsBridge.invokeAction(data);}"; is inject when the page is loaded it show error its' normal. But When i try directly on the button with "inline:invokeCSharpAction("dataTest"), that's not work.

    I try to find a way to get the context of JS to see if the JS injection is well done.

  • theocangtheocang Member ✭✭✭

    Ok so my issue it's patched it came from my website in ASP Core.

Sign In or Register to comment.