Xamarin Android点击手势事件处理程序无法触发

时间:2019-05-14 19:36:29

标签: c# android .net xamarin xamarin.android

我正在学习Xamarin Android,并向应用程序中添加了一个制表符手势。以下是我的MainPage.xaml.cs代码:

namespace WorkingWithWebview
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();   
            webView.Source = "https://www.google.com/";    
            var tapGestureRecognizer = new TapGestureRecognizer();    
            tapGestureRecognizer.NumberOfTapsRequired = 1;    
            tapGestureRecognizer.Tapped += (s, e) => {
                Debug.Print("Tap event!");
            };
            webView.GestureRecognizers.Add(tapGestureRecognizer);
        }        
    }
}

我正在使用WebView来显示页面并将点击手势绑定到该页面。点击手势代码完全来自MS官方文档: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap

以下是我的MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:WorkingWithWebview"
             x:Class="WorkingWithWebview.MainPage">
    <StackLayout>
        <WebView x:Name="webView" WidthRequest="1000" HeightRequest="1000"/>
    </StackLayout>
</ContentPage>

但是由于某些原因,无法触发事件处理程序Debug.Print()函数。怎么解决呢?

2 个答案:

答案 0 :(得分:0)

这种方法有点笨拙,但是您可以将Focus事件添加到WebView并在该事件中执行所需的操作。

<WebView Focused="WebView_Focused"/>
private void WebView_Focused(object sender, FocusEventArgs e)
{
    // Insert Logic Here
}

注意:此实现看起来只会在第一个焦点上激活Focus事件。如果由于某种原因需要多次轻按,请尝试-

<WebView x:Name="MyWebView" Focused="WebView_Focused" />
private void WebView_Focused(object sender, FocusEventArgs e)
{
    // Your Logic Here
    MyWebView.Unfocus();
}

答案 1 :(得分:0)

您可以使用自定义渲染器覆盖 OnTouch 方法以获得效果:

首先在您的共享代码中创建一个自定义WebView MyWebView.cs

public class MyWebView:WebView
{
}

然后在 .Android 中创建MyWebViewRenderer.cs

using Android.Content;
using Android.Icu.Util;
using Android.Util;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using App18;
using App18.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.AppCompat;
using View = Android.Views.View;
[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace App18.Droid
{
  class MyWebViewRenderer : ViewRenderer<MyWebView, Android.Webkit.WebView>,View.IOnTouchListener
   {
      private Context _context;
      private float preX;
      private float curX;
      private static int MIN_CLICK_DELAY_TIME = 1000;
      private long lastClickTime = 0;
      public MyWebViewRenderer(Context context) : base(context)
       {
        _context = context;
       }

      public bool OnTouch(View v, MotionEvent e)
       {
        if (e.Action == MotionEventActions.Down)
         {
            Toast.MakeText(_context, "click", ToastLength.Short).Show();
            preX = e.RawX;

         }

        if (e.Action == MotionEventActions.Up)
         {
            curX = e.RawX;
            long currentTime = Calendar.Instance.TimeInMillis;
            if (currentTime - lastClickTime < MIN_CLICK_DELAY_TIME)
            {

                Toast.MakeText(_context, "double click", ToastLength.Short).Show();
            }
            lastClickTime = currentTime;

            Log.Error("time", currentTime + "-----------" + lastClickTime);
            //the distance you could define your own,here is 200,and just a simple judgment of the x direction
            if ((curX - preX) > 200)
            {
                Toast.MakeText(_context, "swipe right", ToastLength.Short).Show();
            }

            if ((curX - preX) < -200)
            {
                Toast.MakeText(_context, "swipe left", ToastLength.Short).Show();
            }

         }
        return false;
      }

    protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<MyWebView> e)
    {
        base.OnElementChanged(e);
        if (Control == null)
        {
            var webView = new Android.Webkit.WebView(_context);
            SetNativeControl(webView);
            webView.LoadUrl("https://www.google.com");
            webView.SetOnTouchListener(this);
        }
    }
  }
}

最终在page.xaml中使用:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App18"
         x:Class="App18.Page1">
   <ContentPage.Content>
    <StackLayout>
        <local:MyWebView  WidthRequest="1000" HeightRequest="500" ></local:MyWebView>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

这是常规代码段,您可以修改自己的特定要求