【问题标题】:How to get selected elements index of a gridview?如何获取gridview的选定元素索引?
【发布时间】:2019-05-14 15:15:02
【问题描述】:

在这里,我正在创建一个动态网格视图。

    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);
                    }
                }
            }

当用户点击任何帧时,它会打开相机,捕捉后,图像将被设置为选定的帧。

那么如何获取这个网格的选中帧索引呢? 另外如何从这个图像视图中找到图像源?

是否有任何插件可用于使用此类功能以 xamarin 形式上传多张图片?

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    至于我,ClassId 做了一个诡计。

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

    yourframe.ClassId = yourindex;
    

    然后在您的Tap 事件中获取您的View's ClassId。 类似的东西:

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

    【讨论】:

    • 现在我可以设置图片了,谢谢:)
    【解决方案2】:

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

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

    【讨论】:

    • 它工作正常,你能告诉我如何获得选定的项目儿童吗?
    • 在每个选择中,您将获得 Grid 的行或列。如果您可以在选择任何网格部分时向附件类添加属性 ID 并访问
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多