Xamarin Forms CollectionView 分组不使用 Intellisense

Xamarin Forms CollectionView Grouping Not Working With Intellisense

问题

我对 CollectionView 中分组的工作方式感到非常困惑。

我能够使用 Xamarin 文档中的推荐示例和以下代码获得预期的输出:

集合视图XAML

<CollectionView IsGrouped="True" ItemsSource="{Binding CategoriesList}" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5}">
            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Vertical" ItemSpacing="10" />
            </CollectionView.ItemsLayout>
            <CollectionView.GroupHeaderTemplate>
                <DataTemplate>
                    <Label Text="{Binding Name}" />
                </DataTemplate>
            </CollectionView.GroupHeaderTemplate>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <SwipeView>

                        <SwipeView.LeftItems>
                            <SwipeItems>
                                <SwipeItem Text="Delete" IconImageSource="CategoryPhoto" />
                            </SwipeItems>
                        </SwipeView.LeftItems>
                        <SwipeView.RightItems>
                            <SwipeItems>
                                <SwipeItem Text="Edit" IconImageSource="CategoryPhoto" />
                            </SwipeItems>
                        </SwipeView.RightItems>

                        <SwipeView.Content>
                            <StackLayout BackgroundColor="Red">
                                <StackLayout.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding Path=BindingContext.EditProduct, Source={x:Reference Name=MainView}}" CommandParameter="{Binding ID}" />
                                </StackLayout.GestureRecognizers>
                                <StackLayout Orientation="Horizontal" Padding="15">
                                    <Image Source="CategoryPhoto" />
                                    <StackLayout Orientation="Vertical">
                                        <Label Text="{Binding Name}" TextColor="Black" />
                                        <Label Text="doy" TextColor="Black" />
                                    </StackLayout>
                                    <StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand">

                                        <Label Text="&#8854;" TextColor="Black" VerticalTextAlignment="Center" RelativeLayout.HeightConstraint="{ConstraintExpression Property=Height, Factor=1, Type=RelativeToParent}">
                                            <Label.GestureRecognizers>
                                                <!--<TapGestureRecognizer Command="{Binding Path=BindingContext.RemoveItem, Source={x:Reference Name=MainView}}" CommandParameter="{Binding ID}" />-->
                                            </Label.GestureRecognizers>
                                        </Label>

                                        <Label x:Name="QuantityLabel" Text="{Binding Quantity}" TextColor="Black" VerticalTextAlignment="Center" />

                                        <Label Text="&#8853;" TextColor="Black" VerticalTextAlignment="Center">
                                            <Label.GestureRecognizers>
                                                <!--<TapGestureRecognizer Command="{Binding Path=BindingContext.PurchaseItem, Source={x:Reference Name=MainView}}" CommandParameter="{Binding ID}" />-->
                                            </Label.GestureRecognizers>
                                        </Label>

                                    </StackLayout>
                                </StackLayout>
                                <Rectangle Fill="LightGray" HeightRequest="2" />
                            </StackLayout>
                        </SwipeView.Content>

                    </SwipeView>
                
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

产品Class

using System;
using System.ComponentModel;
using SQLite;
using SQLiteNetExtensions.Attributes;

namespace TakeItAwayPOS.Structures
{
    public class Product
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }

        public string Name { get; set; }
        public double Price { get; set; }
        public int Stock { get; set; }

        [ForeignKey(typeof(Category))]
        public int Category { get; set; }

        //for adding to orders
        [Ignore]
        private int quantity { get; set; }
        public int Quantity
        {
            get => quantity;
            set { quantity = value; QuantityChanged(); }
        }

        public delegate void PropertyChangedEvent(object sender, PropertyChangedEventArgs args);
        public event PropertyChangedEvent PropertyChanged;

        private void QuantityChanged()
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
        }


    }
}

类别组Class

using System;
using System.Collections.Generic;

namespace TakeItAwayPOS.Structures
{
    public class CategoryGroup : List<Product>
    {
        public string Name { get; private set; }
        public CategoryGroup(string name, List<Product> products) : base(products)
        {
            Name = name;
        }
    }
}

编辑产品命令

public ICommand EditProduct
        {
            get
            {
                return new Command<string>((param) =>
                {
                    Console.WriteLine("Triggered!");
                    LoadProduct(Convert.ToInt32(param));

                });
            }
        }

我的问题是 GestureRecogniser 用于 DataTemplate 内部的 StackLayout:

<StackLayout.GestureRecognizers>
   <TapGestureRecognizer Command="{Binding Path=BindingContext.EditProduct, Source={x:Reference Name=MainView}}" CommandParameter="{Binding ID}" />
</StackLayout.GestureRecognizers>

CommandParameter 绑定不起作用,只有当我将 "{Binding ID}" 替换为 "0" 时才会触发命令:

当我将鼠标悬停在 CollectionView 中的所有绑定项上时,智能感知将它们引用到 CategoryGroup 类型而不是预期的 Product 类型,奇怪的是另一个除了命令之外的绑定值都有效,即使智能感知将它们引用到错误的类型。

我的两个问题是:

  1. 我将如何尝试修复该命令
  2. 我能否修复 intellisense 以便它识别分组何时为真(或者这是一个已知问题?)

提前致谢!

已解决 谢谢@Jason!

出于某种原因,在我的代码前面,CommandParameter 不想作为 int 工作,所以我使用了这种冗长的方法,该方法转换为字符串,然后返回到Command 的 lambda 方法中的 int。结果我不需要这个,修复只是将命令的参数类型更改为 int 如下:

public ICommand EditProduct
        {
            get
            {
                return new Command<int>((param) =>
                {
                    Console.WriteLine("Triggered!");
                    LoadProduct(param);

                });
            }
        }

ID是一个int,所以你需要使用Command<int>,而不是Command<string>