I Get error in The Output of Visual Studio:
Uncaught TypeError: Foo.run is not a function
When Trying to Run JavaScript function in Index.html to Call C# Method:
<button type="button" onClick="Foo.run()">Click Me!</button>
I tried to Add Notation:
[JavascriptInterface]
[Export("Run")]
It didn't work i use CrossWalk WebView so i Added :
[Org.Xwalk.Core.JavascriptInterface]
[Export("Run")]
Here my complete Code in C#:
using Android.App;
using Android.Widget;
using Android.OS;
using System;
using Android.Views;
using Android.Webkit;
using Java.Interop;
using Android.Content;
namespace XWalk
{
[Activity(Label = "XWalk", MainLauncher = true)]
public class MainActivity : Org.Xwalk.Core.XWalkActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
}
protected override void OnXWalkReady()
{
var view = new RelativeLayout(this.BaseContext);
var mp = ViewGroup.LayoutParams.MatchParent;
var xwv = new Org.Xwalk.Core.XWalkView(this.BaseContext, this);
view.AddView(xwv);
this.AddContentView(view, new ViewGroup.LayoutParams(mp, mp));
xwv.AddJavascriptInterface(new Foo(this), "Foo");
xwv.LoadUrl("file:///android_asset/index.html");
}
class Foo : Java.Lang.Object, Java.Lang.IRunnable
{
Context context;
public Foo(Context context)
{
this.context = context;
}
[Org.Xwalk.Core.JavascriptInterface]
//[Export]
///[JavascriptInterface]
[Export("Run")]
public void Run()
{
Toast.MakeText(context, "This is a Toast from C#!", ToastLength.Short).Show();
}
}
}
}
I'm Targeting Android 4.1 and Above so i Used
Java.Lang.IRunnable
As the Xamarin Documentation Mentioned But the Result in The Output When Click The HTML Button:
Uncaught TypeError: Foo.run is not a function
update
for any one who wants to build it here how its worked with me:
download the project : https://github.com/KevinGliewe/Xamarin.Android.XWalk
now the project not use share crosswalk library but i want it share library so what i did is edit this file "XWalk.Binding.csproj" in project after i extract the whol project and change :
<ItemGroup>
<LibraryProjectZip Include="Jars\xwalk_core_library-23.53.589.4-x86.aar" />
</ItemGroup>
to :
<ItemGroup>
<LibraryProjectZip Include="Jars\xwalk_shared_library-23.53.589.4.aar" />
</ItemGroup>
and download the shared library from: https://download.01.org/crosswalk/releases/crosswalk/android/maven2/org/xwalk/
and put it in Jars folder inside XWalk.Binding project
after that open the solution project just build the whole project and run it .. if you want release version what worked with me make sure the options in XWalk.Binding project properties in build tab the "define Debug constant" and "define trace constant" are checked or it will not detect the crosswalk library when working in c# codes
and build it and run and it will works...