【问题标题】:Adding 2 TapGestureRecognizer not possible?无法添加 2 个 TapGestureRecognizer?
【发布时间】:2017-12-03 11:37:24
【问题描述】:

我的自定义控件上有 2 个 TapGestureRecognizer:

1) 第一个是内部的/仅存在于自定义控件中。

2) 第二个附加在实例化自定义控件的页面上。

我正在使用第一个 TapGestureRecognizer 在内部/自定义控件内的 Tap 处触发动画,第二个 TapGestureRecognizer 用于跟踪页面上自定义控件上的 Taps,以便我可以对点击做出反应。

在页面“外部”/ 上做动画感觉不对,因为这个控件的每个实例都应该动画,这就是我在自定义控件中附加 TapGestureRecognizer 的原因。

但是,当我这样做时,只有“内部”的 TapGestureRecognizer 起作用,而外部的则不起作用。

这是正常行为吗?

public class clsGridCell : ContentView
{

     var _Grid1ContainerForImageAndLabel = new Grid()
     {
     }

     var nTapRec = new TapGestureRecognizer();
     nTapRec.Tapped += OnItemSelected;
     _Grid1ContainerForImageAndLabel.GestureRecognizers.Add(nTapRec);

     this.Content = _Grid1ContainerForImageAndLabel;

     }

     private async void OnItemSelected(object sender, EventArgs e)
     {
         await Task.WhenAny<bool>
         (
            _image1.ScaleTo(0.9, 50, Easing.Linear)
         );

        //run some background color animation, too
    }

和“外”/在页面上:

public class MainPage : ContentPage
{
     var nGridCell = new clsGridCell                            
     {
         ImageSource = nImgSrc,
         BackgroundColor = Color.Blue;
     };

     _BigGrid.Children.Add(nGridCell);

     var nTapRec = new TapGestureRecognizer();
     nTapRec.Tapped += OnItemSelected;
     nGridCell.GestureRecognizers.Add(nTapRec);

    private async void OnItemSelected(object sender, EventArgs e)
    {
         //Not fired! When I remove the "internal" TapGestureRecognizer, it does work

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    只需将内部 TapGestureRecognizer 创建为类的公共/内部属性,而不是在“外部”创建新的手势,而是向类的 TapGestureRecognizer 添加新的点击动作。像这样:

    public class clsGridCell : ContentView     
    { 
        public TapGestureRecognizer TapGesture { get; set; }
    
        Action<clsGridCell> tap;
        public Action<clsGridCell> Tap
        {
            get => tap;
            set
            {
                tap = value;
                TapGesture.Tapped += (sender, e) => { value(this); };
            }
        }
    
        public clsGridCell()
        {
            var _Grid1ContainerForImageAndLabel = new Grid() { };
    
            TapGesture = new TapGestureRecognizer(); 
            TapGesture.Tapped += OnItemSelected; 
            _Grid1ContainerForImageAndLabel.GestureRecognizers.Add(TapGesture); 
    
            this.Content = _Grid1ContainerForImageAndLabel; 
        } 
    
        private async void OnItemSelected(object sender, EventArgs e) 
        { 
            await Task.WhenAny<bool> ( _image1.ScaleTo(0.9, 50, Easing.Linear) ); //run some background color animation, too
        }
    }
    

    和外部使用myGrid.Tap = Method;

    【讨论】:

    • 谢谢,但是在外部调用 private async void MyMethod(object sender, EventArgs e) 时,发送者是“Xamarin.Forms.Grid”而不是 clsGridCell。我无法访问我在 clsGridCell 中定义的其他属性。我只能访问 Xamarin.Forms.Grid 的属性。这不是我需要的。
    • 我更新了答案,有了这个更正你会得到你想要的,希望对你有帮助:)
    • 你能告诉我如何声明“方法”吗?我试过“private async void MyMethod(object sender, EventArgs e)”,它会引发编译器错误“MyMethod 没有重载符合 Action
    • 当然,它不再是同一个代表了,这样做:MyMethod(clsGridCell cell)。或者,如果您更喜欢内联:(cell) =&gt; { };
    • 非常感谢。我认为“对象发送者,EventArgs e”是通用的,适用于任何事情。
    猜你喜欢
    • 1970-01-01
    • 2022-11-29
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多