简体   繁体   English

在Android Xamarin CrossWalk Webview中从Javascript调用C#函数

[英]Call C# Function From Javascript in Android Xamarin CrossWalk Webview

I Get error in The Output of Visual Studio: 我在Visual Studio的输出中得到错误:

     Uncaught TypeError: Foo.run is not a function

When Trying to Run JavaScript function in Index.html to Call C# Method: 尝试在Index.html中运行JavaScript函数以调用C#方法时:

     <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 : 我使用CrossWalk WebView无效,所以我添加了:

         [Org.Xwalk.Core.JavascriptInterface]
         [Export("Run")] 

Here my complete Code in C#: 这是我在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 我的目标是Android 4.1及更高版本,所以我用了

      Java.Lang.IRunnable

As the Xamarin Documentation Mentioned But the Result in The Output When Click The HTML Button: 正如Xamarin文档所提到的那样,但是单击HTML按钮时输出结果:

      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 下载项目: 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 : 现在该项目不使用共享人行横道库,但我希望它共享库,因此我提取了whol项目并更改后,在项目中编辑此文件“ XWalk.Binding.csproj”:

<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/ 并从以下网址下载共享库: https : //download.01.org/crosswalk/releases/crosswalk/android/maven2/org/xwalk/

and put it in Jars folder inside XWalk.Binding project 并将其放在XWalk.Binding项目内的Jars文件夹中

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 之后,打开解决方案项目,只需构建整个项目并运行它即可..如果要发行的版本与我一起工作,请确保在XWalk.Binding选项卡中的“构建”选项卡中的项目属性为“定义调试常数”和“定义跟踪常数”在使用C#代码工作时被选中或无法检测到人行横道库

and build it and run and it will works... 并构建并运行,它将起作用...

<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 我试着这样称呼它,并且奏效了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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