简体   繁体   中英

How to call JavaScript from C# in hybridwebview in Xamarin PCL?

I am very new to Xamarin Cross platform app development, using VS 2015. My primary requirement was to get values from javascript function to C#, so I started working with HybridWebView. With the help of official guides ( link-1 , link-2 ), I was able to get values from javascript to C#.

But, now I am not able to call javascript from C# using hybridWebView.Eval("jsFunction()") , as mentioned here , but it's showing error now - HybridWebView does not contain definition for Eval

HybridWebView.cs

namespace Medical_Study
{
    public class HybridWebView : View
    {
        Action<string> action;
        public static readonly BindableProperty UriProperty = BindableProperty.Create(
          propertyName: "Uri",
          returnType: typeof(string),
          declaringType: typeof(HybridWebView),
          defaultValue: default(string));

        public string Uri
        {
            get { return (string)GetValue(UriProperty); }
            set { SetValue(UriProperty, value); }
        }

        public void RegisterAction(Action<string> callback)
        {
            action = callback;
        }

        public void Cleanup()
        {
            action = null;
        }

        public void InvokeAction(string data)
        {
            if (action == null || data == null)
            {
                return;
            }
            action.Invoke(data);
        }
    }
}

HybridWebViewPage.cs

namespace Medical_Study
{
    public class HybridWebViewPageCS : ContentPage
    {
        public HybridWebViewPageCS()
        {
            var hybridWebView = new HybridWebView
            {
                Uri = "index.html",
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand
            };

            Padding = new Thickness(0, 20, 0, 0);
            Content = hybridWebView;
        }
    }
}

MCQ.xaml.cs

namespace Medical_Study
{
    public partial class MCQ : ContentPage
    {
        public MCQ()
        {
            InitializeComponent();

            //hybridWebView.RegisterAction(data => DisplayAlert("Alert", "Hello " + data, "OK"));
            hybridWebView.RegisterAction(data => jsTest(data));
        }

        public void jsTest(string jsReturn)
        {
            Debug.WriteLine(jsReturn);
            DisplayAlert("Test Title", "Test Message => " + jsReturn, "OK");
        }

        public void OnCallJavaScriptButtonClicked(object sender, EventArgs e)
        {
            // I am trying to trigger testFunction() but it's showing error - HybridWebView does not contain definition for Eval
            hybridWebView.Eval("testFunction()");
        }

    }
}

How to get working communication both sides (C# to JS and JS to C#)?

You just could invoke c# function in javascript of front end. For example,

JS

<script>
    window.onload = function () {
        invokeCSharpAction('Bill Gates');
    }
</script>

CS

hybridWebView.RegisterAction(name => DisplayAlert("Alert", "Hello " + name, "OK"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM