如何获取GridView的选定元素索引?

时间:2019-05-14 06:54:02

标签: xamarin xamarin.forms

在这里,我正在创建一个动态的gridview。

    private void PopulateImages()
            {
                ObservableCollection<Attachment> Images = new ObservableCollection<Attachment>();
                Images.Add(new Attachment { Name = "Image 1", Index = 0 });
                Images.Add(new Attachment { Name = "Image 2", Index = 1 });
                Images.Add(new Attachment { Name = "Image 3", Index = 2 });
                Images.Add(new Attachment { Name = "Image 4", Index = 3 });
                Images.Add(new Attachment { Name = "Image 5", Index = 4 });
                Images.Add(new Attachment { Name = "Image 6", Index = 5 });

                gridLayout.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
                gridLayout.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });

                gridLayout.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(33, GridUnitType.Star) });
                gridLayout.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(33, GridUnitType.Star) });
                gridLayout.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(33, GridUnitType.Star) });

                gridLayout.ColumnSpacing = 8;
                gridLayout.RowSpacing = 8;

                var productIndex = 0;
                for (int rowIndex = 0; rowIndex < 2; rowIndex++)
                {
                    for (int columnIndex = 0; columnIndex < 3; columnIndex++)
                    {
                        if (productIndex >= Images.Count)
                        {
                            break;
                        }
                        var product = Images[productIndex];
                        productIndex += 1;

                        var frame = new Frame
                        {
                            HeightRequest = 95,
                            WidthRequest = 70,
                            CornerRadius = 10,
                            HasShadow = false,
                            BorderColor = Color.FromHex("#CCCCCC")
                        };

                        var image = new Image
                        {
                            Source = "addImage.png",
                            VerticalOptions = LayoutOptions.Center,
                            HorizontalOptions = LayoutOptions.Center,
                            HeightRequest = 35,
                            WidthRequest = 35
                        };

                        frame.Content = image;

                        TapGestureRecognizer tapped = new TapGestureRecognizer();

                        tapped.Tapped += (s, e) =>
                        {                        
                            TakePhoto();
                        };

                        frame.GestureRecognizers.Add(tapped);

                        gridLayout.Children.Add(frame, columnIndex, rowIndex);
                    }
                }
            }

result

当用户单击任何帧时,它将打开相机,并在捕获后将图像设置为选定的帧。

那么如何获取此网格的选定帧索引? 还有如何从此图像视图中找到图像源?

是否可以使用插件在Xamarin表单中使用这种功能来上传多张图片?

2 个答案:

答案 0 :(得分:1)

对于我来说,ClassId起到了欺骗作用。

在创建您的Frame时,请致电:

yourframe.ClassId = yourindex;

,然后在您的Tap事件中获取您的View's ClassId。 像这样:

tapGestureRecognizer.Tapped += (s, e) => 
{
    // получим свойства для ячейки, чтобы понять, какое всплыващее окно вызвать
    var specIndex= Convert.ToInt32(((View) s).ClassId);                    
};

答案 1 :(得分:0)

您可以使用它来获取所选网格的Row属性和Column属性:

tapped.Tapped += (s, e) =>
{
   TakePhoto();
   var row = (int)((BindableObject)s).GetValue(Grid.RowProperty);
   var column = (int)((BindableObject)s).GetValue(Grid.ColumnProperty);
};