【问题标题】:Dynamic TapGestureRecognizer on StackLayout in XamarinFormsXamarin 表单中 StackLayout 上的动态 TapGestureRecognizer
【发布时间】:2020-06-01 01:50:48
【问题描述】:

我有下面的代码,我基本上是循环一个列表并使用该列表的属性作为值:

SLTData.g_RecentLoans.ForEach(x => {
    colorCode = loanHistoryViewModel.GetStatusColor(x.ActivityListID);
    principal = utilities.FormatCurrency(x.PAMT);
    formattedDate = loanHistoryViewModel.ConvertDate(x.DateRequested);

    stackRecentList.Children.Add(
             new Frame {
                BackgroundColor = Color.White,
                Margin = new Thickness(30, 20, 30, 0),
                Padding = new Thickness(10),
                CornerRadius = 5,
                HasShadow = true,
                Content = new StackLayout {
                    Orientation = StackOrientation.Horizontal,
                    Spacing = 0,
                    Children = {
                        // Content here
                        // The 'x' variables are used inside this StackLayout
                    }
                }
            }
        );
});

问题是,我想在循环中 StackLayout 的每次迭代中添加一个 TapGestureRecognizer。我尝试在 StackLayout 的右括号中添加.GestureRecognizers.Add(tapGestureRecognizer),但它显示Cannot implicitly convert type 'void' to 'Xamarin.Forms.View'

我基本上想要做的是向处理点击的函数添加一个参数,该参数是传递给 API 的关键字。

【问题讨论】:

  • 那是因为你必须将它应用到 Add 方法,检查括号并将它应用到你的 View !!!

标签: c# xamarin.forms uitapgesturerecognizer


【解决方案1】:

我想在循环中StackLayout的每次迭代中添加一个TapGestureRecognizer。

也许你在为StackLayout添加tapGestureRecognizer时遇到了一些问题,我提供了一些代码你可以看看:

  private  void loadtap()
    {
        for(int i=0;i<5;i++)
        {
            Frame f = new Frame()
            {
                BackgroundColor = Color.White,
                Margin = new Thickness(30, 20, 30, 0),
                Padding = new Thickness(10),
                CornerRadius = 5,
                HasShadow = true,

            };

            StackLayout stack = new StackLayout()
            {
                Orientation = StackOrientation.Horizontal,
                Spacing = 0,
                Children = { new Label(){Text="this is test"}

                }
            };

            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (s, e) => {
              DisplayAlert("Alert", "You have been click tap", "OK");
            };

            stack.GestureRecognizers.Add(tapGestureRecognizer);
            f.Content = stack;

            stacklist.Children.Add(f);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 2017-04-30
    • 2017-12-21
    • 2016-09-07
    • 2019-04-02
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多