簡體   English   中英

在Android Xamarin CrossWalk Webview中從Javascript調用C#函數

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

我在Visual Studio的輸出中得到錯誤:

     Uncaught TypeError: Foo.run is not a function

嘗試在Index.html中運行JavaScript函數以調用C#方法時:

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

我試圖添加符號:

        [JavascriptInterface]
        [Export("Run")]  

我使用CrossWalk WebView無效,所以我添加了:

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

這是我在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();
        }
    }

}
}  

我的目標是Android 4.1及更高版本,所以我用了

      Java.Lang.IRunnable

正如Xamarin文檔所提到的那樣,但是單擊HTML按鈕時輸出結果:

      Uncaught TypeError: Foo.run is not a function 

更新

對於任何想要在這里構建它的人如何與我合作:

下載項目: https : //github.com/KevinGliewe/Xamarin.Android.XWalk

現在該項目不使用共享人行橫道庫,但我希望它共享庫,因此我提取了whol項目並更改后,在項目中編輯此文件“ XWalk.Binding.csproj”:

<ItemGroup>
    <LibraryProjectZip Include="Jars\xwalk_core_library-23.53.589.4-x86.aar" />
 </ItemGroup>

至 :

   <ItemGroup>
     <LibraryProjectZip Include="Jars\xwalk_shared_library-23.53.589.4.aar" />
  </ItemGroup>

並從以下網址下載共享庫: https : //download.01.org/crosswalk/releases/crosswalk/android/maven2/org/xwalk/

並將其放在XWalk.Binding項目內的Jars文件夾中

之后,打開解決方案項目,只需構建整個項目並運行它即可..如果要發行的版本與我一起工作,請確保在XWalk.Binding選項卡中的“構建”選項卡中的項目屬性為“定義調試常數”和“定義跟蹤常數”在使用C#代碼工作時被選中或無法檢測到人行橫道庫

並構建並運行,它將起作用...

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

改成:

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

我試着這樣稱呼它,並且奏效了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM