Hary
Hary

Reputation: 5818

Open Signature Pad from Xamarin Hybrid Web View

I have used JSBridge to create the bridge between Hybrid Web View and the c# Action and that works fine.

I am trying to invoke the signature pad view based on the parameters and want to load the signature in the image element inside HTML Page.

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);

      });
     }

I am not sure how to invoke the signatureView on top of the current view. Any ideas?

Upvotes: 0

Views: 98

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10346

According to your description, you want to get local signature pad view from hybridwebview, You can create a C# class that contains methods to be called from 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();
}

}

And then call in JS:

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

You can take a look the following article:

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

Upvotes: 1

Related Questions