0

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...

6
  • could it work ? Commented Jan 11, 2019 at 6:25
  • ya its work but you need binding java to c# github.com/KevinGliewe/Xamarin.Android.XWalk Commented Jan 11, 2019 at 16:20
  • Is foo.run () or foo.run () working now? Commented Jan 14, 2019 at 5:34
  • yes its work its work thank you Foo.Run() is working Commented Jan 14, 2019 at 20:52
  • I am very glad that it has helped you. If you could give me a mark, I will be very grateful Commented Jan 15, 2019 at 1:19

1 Answer 1

1
<button type="button" onClick="Foo.run()">Click Me!</button>

change to:

<button type="button" onClick="Foo.Run()">Click Me!</button>

I tried calling it like this, and it worked

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.