Calling C# from JavaScript in Xamarin

Hasan A Yousef

Trying to test this example, but found it not working with me, I use API19, and my code is:

using System;
using Android.App;
using Android.Content;  
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using Java.Interop;

namespace App3
{
[Activity(Label = "App3", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    int count = 1;

    const string html = @"
         <html>
         <body>
             <p>Demo calling C# from JavaScript</p>
             <button type=""button"" onClick=""CSharp.ShowToast()"">Call C#   </button>
        </body>
    </html>";

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);

        WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView);
        localWebView.SetWebViewClient(new WebViewClient()); // stops request going to Web Browser
        localWebView.Settings.JavaScriptEnabled = true;
        // localWebView.LoadUrl("http://developer.xamarin.com");
        // localWebView.LoadUrl("file:///android_asset/index.html");
        localWebView.LoadData(html, "text/html", null);

        button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
    }
}
    class MyJSInterface : Java.Lang.Object
    {
        Context context;

        public MyJSInterface(Context context)
        {
            this.context = context;
        }

        [Export]
        [JavascriptInterface]
        public void ShowToast()
        {
            Toast.MakeText(context, "Hello from C#", ToastLength.Short).Show();
        }
    }
}

What is the mistake I've here!

Note: I already Added a reference to Mono.Android.Export (so you can use the [Export] annotation):

matthewrdev

You need to add an instance of the MyJavascriptInterface to the localWebView before loading the HTML:

WebView localWebView = FindViewById<WebView>(Resource.Id.LocalWebView);
localWebView.SetWebViewClient(new WebViewClient()); // stops request going to Web Browser
localWebView.Settings.JavaScriptEnabled = true;

// Add an instance of the Javascript interface named "CSharp"
localWebView.AddJavascriptInterface(new MyJSInterface(this), "CSharp");

localWebView.LoadData(html, "text/html", null);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a C# method from JavaScript

From Dev

Call JavaScript from C# object in Xamarin

From Dev

Calling Dart from Javascript

From Dev

Calling Servlet from JavaScript

From Dev

Calling Swift from JavaScript

From Dev

Calling JavaScript from Android

From Dev

Calling JavaScript from JSP

From Dev

Calling a C style function pointer in a WebAssembly from JavaScript

From Javascript

Calling JavaScript from C++ with node.js

From Dev

Calling C# component from JavaScript in Windows 10 Universal App

From Dev

Blazor JS interop - calling C# instance methods from javascript

From Dev

Calling javascript function from C# code behind

From Dev

Calling a C# function through Javascript onClick event in WebView in Xamarin.Forms

From Dev

Calling Swift from C

From Dev

calling system() from c

From Dev

Calling Qt slot from JavaScript

From Dev

Calling Grails def from Javascript

From Javascript

Calling a Javascript Function from Console

From Javascript

Calling JavaScript Function From CodeBehind

From Javascript

Calling Javascript from a html form

From Java

calling a java servlet from javascript

From Dev

calling javascript from clojurescript (canvasjs)

From Dev

Calling a Javascript function from .cs

From Dev

Calling JavaScript files from VBA

From Dev

Calling JavaScript function from [WebMethod]

From Dev

Calling JavaScript functions from TypeScript

From Dev

Calling JavaScript methods from typescript

From Dev

Calling a URL handler from javascript

From Dev

Calling javascript from codebehind not working