如何使用Xamarin表单将点击事件与对象参数附加在一起

时间:2018-10-28 04:18:08

标签: xamarin xamarin.forms

我正在尝试创建一个stackLayouts列表,我想知道单击了哪个stackLayout。

PFB我试图用来创建stackLayout的代码,

foreach(var package in profileEmailAddressViewModel.Employee.Packages)
        {
            var stackLayoutEmail = new StackLayout
            {
                Orientation = StackOrientation.Horizontal
            };

                var labelEmployerEmail = new Label
                {
                    FontSize = 17,
                    TextColor = Color.FromHex("#030303"),
                    Margin = new Thickness(15, 12, 0, 12),
                    HeightRequest = 20,
                    HorizontalOptions = LayoutOptions.FillAndExpand
                };

                labelEmployerEmail.Text = package.WorkEmailAddress;

                var imageListItem = new Image
                {
                    Source = "arrow.png",
                    Margin = new Thickness(0, 0, 15, 0),
                    HorizontalOptions = LayoutOptions.End,
                    HeightRequest = 16,
                    VerticalOptions = LayoutOptions.Center
                };

                stackLayoutEmail.Children.Add(labelEmployerEmail);
                stackLayoutEmail.Children.Add(imageListItem);

                var tapUpdateEmailEvent = new TapGestureRecognizer();
                tapUpdateEmailEvent.Tapped += (s, e) => {
                    // How to pass in the package parameter value with tapped event here?
                };

                stackLayoutEmail.GestureRecognizers.Add(tapUpdateEmailEvent);


            AddressLayout.Children.Add(stackLayoutEmail);

        }

如何在此处通过点击事件传递包参数值?

请让我知道运行for循环创建列表时附加参数的方式。

1 个答案:

答案 0 :(得分:1)

您可以在CommandParameter上使用TapGestureRecognizer

注意:

理想情况下,应使用ListView处理。只需将ItemsSource设置为Packages

如果您将坚持使用此方法,则最好仅在构造函数中创建一个一个 TapGestureRecognizer,并将其添加到所有StackLayout的手势中