我正在使用Xamarin.Forms网格应用程序在网格CardGrid的单元格中显示几个html元素作为WebViews:
private async void CreateCardView() { CardGrid.Children ….
的 原因: 强>
财产 Focus 在Forms中对应我们调用的方法 becomeFirstResponder 在原生iOS平台上。遗憾的是,UIWebView和WKwebview不支持该方法 becomeFirstResponder 。这种方法仅适用于某些“输入控件”,例如UITextField和UITextView(表单中的条目)。即使你在一个'输入控件'上设置事件 webview 。它不适用于iOS。
Focus
becomeFirstResponder
webview
的 解决方法: 强>
你可以添加一个 TapGestureRecognizer 在webview上。你必须使用CustomRenderer来实现它。因为如果你在表单中添加TapGestureRecognizer会产生冲突。 请参阅以下代码。
TapGestureRecognizer
在表格中
public MainPage() { if (Device.RuntimePlatform == "iOS") { MessagingCenter.Subscribe<Object,string>(this,"webview_click", (sender,arg)=> { // int index = Convert.ToInt16(arg)); }); } }
在iOS中
using System; using Foundation; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; using xxx; using xxx.iOS; using ObjCRuntime; [assembly:ExportRenderer(typeof(WebView),typeof(MyWebViewRenderer))] namespace xxx.iOS { public class MyWebViewRenderer : WebViewRenderer, IUIGestureRecognizerDelegate { public bool isFirstLoad = true; public MyWebViewRenderer() { } protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); if (e != null) { UITapGestureRecognizer tap = new UITapGestureRecognizer(this, new Selector("Tap_Handle:")); tap.WeakDelegate = this; this.AddGestureRecognizer(tap); } } [Export("gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:")] public bool ShouldRecognizeSimultaneously(UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer) { return true; } [Export("Tap_Handle:")] void Tap_Handle(UITapGestureRecognizer tap) { if(isFirstLoad) { isFirstLoad = false; MessagingCenter.Send<Object,string>(this, "webview_click",Element.AutomationId); } } } }