Friday, 22 April 2016

Invoke C# from Javascript in Android using Export attribute

I've been happily invoking C# from a webview in my Xamarin Android app for some while now, but this time I needed to send a parameter from my js. Using the basic Run() was no longer an option and so I turned to a solution using the Export attribute, which I first read about here.
Instead of:
public void Run()
    {
        //do your stuff
    }
you do this:
[Export("customInvoke")]
public void CustomInvoke(string myParameter)
    {
       //do your stuff
    }