I had to ad boxview so i can tap the webview
Actually , it is necessary to use BoxView . The best way is to use CustomRenderer to add a TapGestureRecognizer and let webview recognize it.
in Forms
using System;
using Xamarin.Forms;
namespace xxx
{
public class MyWebView:WebView
{
public MyWebView()
{
}
public event EventHandler Touched;
public void OnTouched() =>
Touched?.Invoke(this, null);
}
}
in Android
[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace xxx.Droid
{
public class MyWebViewRenderer : WebViewRenderer
{
public MyWebViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
Control.Touch += (object sender, TouchEventArgs eventArgs) => {
if (eventArgs.Event.Action == Android.Views.MotionEventActions.Down)
{
var webview = Element as MyWebView;
webview.OnTouched();
}
};
}
}
}
in iOS
using Foundation;
using UIKit;
using xxx;
using xxx.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ObjCRuntime;
[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace xxx.iOS
{
public class MyWebViewRenderer:WkWebViewRenderer, IUIGestureRecognizerDelegate
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (NativeView != null)
{
UITapGestureRecognizer tapGestureRecognizer = new UITapGestureRecognizer(this, new Selector("HandleTap:"))
{
WeakDelegate = this,
CancelsTouchesInView = false
};
NativeView.AddGestureRecognizer(tapGestureRecognizer);
}
}
[Export("gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:")]
public bool ShouldRecognizeSimultaneously(UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
{
return true;
}
[Export("HandleTap:")]
void HandleTap(UITapGestureRecognizer tap)
{
var webview = Element as MyWebView;
webview.OnTouched();
}
}
}
Now you just need to use it like
<local:MyWebView x:Name="browser"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"
Touched = "xxx">
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…