从Xamarin Hybrid Web View打开签名板

时间:2019-06-09 08:21:54

标签: xamarin xamarin.forms xamarin.android

我已经使用JSBridge在Hybrid Web View和c#Action之间创建了桥梁,并且工作正常。

我试图根据参数调用签名板视图,并希望将签名加载到HTML页面内的图像元素中。

public class JSBridge: Java.Lang.Object {
   readonly WeakReference < HybridWebViewRenderer > hybridWebViewRenderer;
   WebView _webView;

   public JSBridge(HybridWebViewRenderer hybridRenderer, WebView webView) {
    hybridWebViewRenderer = new WeakReference < HybridWebViewRenderer > (hybridRenderer);

    _webView = webView;
   }

   [JavascriptInterface]
   [Export("invokeAction")]
   public async void InvokeAction(string data) {

     if (data == "OpenSignaturePad") {

      var signatureView = new SignaturePadView() {
       StrokeWidth = 3 f,
        StrokeColor = Color.White,
        BackgroundColor = Color.Black
      };

      var bitmap = signatureView.GetImageStreamAsync(SignatureImageFormat.Png);

      var url = ImageToBase64(bitmap);

      Xamarin.Forms.Device.BeginInvokeOnMainThread(async () => {
       _webView.EvaluateJavascript("document.getElementById('imgTest').src='" + url + "'", null);

      });
     }

我不确定如何在当前视图顶部调用signatureView。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

根据您的描述,您想要从hybridwebview获取本地签名板视图,您可以创建一个C#类,其中包含要从JavaScript调用的方法,

class MyJSInterface : Java.Lang.Object
{
Context context;

public MyJSInterface (Context context)
{
    this.context = context;
}

public void ShowToast ()
{
    Toast.MakeText(context, "url from signature pad", ToastLength.Short).Show();
}

}

然后调用JS:

<button type="button" onClick="CSharp.ShowToast ('Call C#')">Call C#</button>

您可以看看以下文章:

https://github.com/xamarin/docs-archive/tree/master/Recipes/android/controls/webview/call_csharp_from_javascript