简体   繁体   English

xamarin.forms webview.focused 事件在 Android 上引发,但不在 iOS 上引发

[英]xamarin.forms webview.focused event raised on Android, but not on iOS

I'm using a Xamarin.Forms grid application to show a couple of html elements as WebViews in the cells of the grid CardGrid:我正在使用 Xamarin.Forms 网格应用程序在网格 CardGrid 的单元格中将几个 html 元素显示为 WebView:

    private async void CreateCardView()
    {
        CardGrid.Children.Clear();

        // idx over all count elements of html snippets
        for (idx = 0; idx < count; idx++)
        {
            string html = AuthoringCard(idx);
            RenderingCard(html, idx);
        }
    }

AuthoringCard() creates the html code snippet. AuthoringCard() 创建 html 代码片段。

RenderingCard() creates the WebView inside the grid cell. RenderingCard() 在网格单元内创建 WebView。

    private void RenderingCard(string htmlCard, int index)
    {
        int CardWidth = 300;
        int CardHeight = 150;
        int CardNoHorizontally = 3;

        WebView uiCard = new WebView();
        uiCard.HeightRequest = CardHeight - 5;
        uiCard.WidthRequest= CardWidth - 5;
        uiCard.VerticalOptions = LayoutOptions.Start;
        uiCard.HorizontalOptions = LayoutOptions.Center;
        uiCard.Margin = new Thickness(0);
        uiCard.AutomationId = index.ToString();
        uiCard.Focused += Card_Tapped;
        uiCard.InputTransparent = false;

        var htmlSource1 = new HtmlWebViewSource
        {
            Html = htmlCard,
        };
        uiCard.Source = htmlSource1;

        CardGrid.Children.Add(uiCard);

        int row = (int)Math.Floor((double)(index / CardNoHorizontally));
        int column = index - (row * CardNoHorizontally);

        Grid.SetRow(uiCard, row);
        Grid.SetColumn(uiCard, column);
    }

I want to catch the Focused event, when the user it tapping on the card (WebView) and using the AutomationId to get the index of the card (html code snippet):我想捕捉 Focused 事件,当用户点击卡片 (WebView) 并使用 AutomationId 获取卡片的索引(html 代码片段)时:

    private void Card_Tapped(object sender, EventArgs e)
    {
        WebView card = (WebView)sender;

        int index = Convert.ToInt16(card.AutomationId));
    }

This works fine with Android.这适用于Android。 Under iOS the event is never raised.在 iOS 下,事件永远不会引发。 Any idea for a solution?有什么解决办法吗?

Cause:原因:

the property Focus in Forms correspond method that we called becomeFirstResponder in native iOS platform.Unfortunately,UIWebView and WKwebview do not support the method becomeFirstResponder .This method is only available in some 'input-controls' Such as UITextField and UITextView(Entry in Forms).So even if you set the event on a webview .It will not work in iOS. Forms中的Focus属性对应我们在原生iOS平台调用的becomeFirstResponder方法。可惜UIWebView和WKwebview不支持becomeFirstResponder方法。这个方法只在一些'input-controls'中可用,比如UITextField和UITextView(Entry in Forms) .所以即使你在webview上设置事件。它在 iOS 中也不起作用。

Workaround:解决方法:

You can add a TapGestureRecognizer on the webview.And you have to implement it by using CustomRenderer.Because it will create conflict if you add TapGestureRecognizer in forms.你可以在TapGestureRecognizer上添加一个TapGestureRecognizer 。你必须使用CustomRenderer来实现它。因为如果你在表单中添加TapGestureRecognizer会产生冲突。 Refer to the following code.参考以下代码。

in Forms在表格中

public MainPage()
{
 if (Device.RuntimePlatform == "iOS")
   {

      MessagingCenter.Subscribe<Object,string>(this,"webview_click", (sender,arg)=> {

     // int index = Convert.ToInt16(arg));

     });
   }
}

in iOS在 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);
        }
    }

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS上为内部导航引发的Xamarin.Forms.WebView.Navigating事件 - Xamarin.Forms.WebView.Navigating event raised on iOS for internal navigation Xamarin.Forms iOS编辑日历事件 - Xamarin.Forms ios edit calendar event Xamarin.Forms在Android和IOS中分离设计 - Xamarin.Forms seperation of design in Android and IOS 处理xamarin.forms上的iOS和Android差异 - Dealing with iOS and Android diferences on xamarin.forms 是否可以在iOS和/或Android上通过WebView分层其他Xamarin.Forms控件? - Is it possible to layer other Xamarin.Forms controls over WebView on iOS and/or Android? Xamarin.forms(或)xamarin.ios / xamarin.android(或)本机 - Xamarin.forms (or) xamarin.ios/xamarin.android (or) Native iOS 11.1.2上的Xamarin.Forms:Webview加载了更大的字体 - Xamarin.Forms on iOS 11.1.2: Webview loads a bigger font Webview 总是在 ios Xamarin.Forms 中返回空白屏幕 - Webview always return blank screen in ios Xamarin.Forms 如何在 Xamarin.Forms 中获取 iOS 和 Android 的辅助功能字体大小,以便我可以在 HTML WebView 中更改字体大小? - How to get the accessibility font size for iOS and Android in Xamarin.Forms so I can change font-size in a HTML WebView? Xamarin.Forms - 在iOS中不更新属性赋值的Slider,适用于Android - Xamarin.Forms - Slider not updating on property assignment in iOS, works in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM