如何从 Xamarin 中的首选项中对数据列表进行分组

How to group data list from Preferences in Xamarin

我在首选项中存储了如下订单信息列表:

public class CartUser
{       
    public int IDProduct { get; set; }
    public string NameProduct { get; set; } 
    public string SupplierID { get; set; }
}

我想显示一个SupplierID组的列表,我想到了使用Group By的方案

PageOne.xaml.cs

List<CartUser> cartUsers = new List<CartUser>();
var mycart = Preferences.Get("CartUserAdds", "_mycart");
var getcart = JsonConvert.DeserializeObject<List<CartUser>>(mycart).GroupBy(x => x.SupplierID);
cartUsers = (List<CartUser>)getcart;
BindableLayout.SetItemsSource(stdata, cartUsers);

但是我得到错误:System.InvalidCastException: 'Specified cast is not valid.' 右行 cartUsers = (List<CartUser>)getcart;

PageOne.xaml

<StackLayout x:Name="stdata">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <StackLayout x:DataType="model:CartUser">
                <Label Text="{Binding SupplierID}"/>
                <Label Text="{Binding NameProduct}"/>
            </StackLayout>
        </DataTemplate>
    </BindableLayout.ItemTemplate>                            
</StackLayout>

我取自Preferences的数据:

[{\"IDProduct\":1,\"NameProduct\":\"Name product 1\",\"SupplierID\":\"22379356\"},{\"IDProduct\":2,\"NameProduct\":\"Name product 2\",\"SupplierID\":\"22379356\"},{\"IDProduct\":3,\"NameProduct\":\"Name product 3\",\"SupplierID\":\"12336544\"}]

我要这样显示

我读了这篇文章:。但是它设置了 ListView 中的显示。我希望它显示在 StackLayout

期待大家的解答。谢谢!

使用 CollectionView 更新

SupplierIDGrouping.cs

public class SupplierIDGrouping : ObservableCollection<CartUser>
{
    public string SupplierID { get; private set; }

    public SupplierIDGrouping(string supplierID)
    : base()
    {
        SupplierID = supplierID;
    }

    public SupplierIDGrouping(string supplierID, IEnumerable<CartUser> source)
        : base(source)
    {
        SupplierID = supplierID;
    }

}

PageOne.xaml

<CollectionView ItemsSource="{Binding SupplierList}" IsGrouped="true">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Label Text="{Binding NameProduct}"/>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

PageOne.xaml.cs

public ObservableCollection<SupplierIDGrouping> SupplierList { get; private set; } = new ObservableCollection<SupplierIDGrouping>();

List<CartUser> cartUsers = new List<CartUser>();
var mycart = Preferences.Get("CartUserAdds", "_mycart");
var getcart = JsonConvert.DeserializeObject<List<CartUser>>(mycart);
cartUsers = getcart;

foreach (var item in cartUsers)
{
    if (!SupplierList.Any(supplierid => supplierid.SupplierID == item.SupplierID))
    {
        SupplierList.Add(new SupplierIDGrouping(item.SupplierID));
    }

    SupplierList.Single(supplierid => supplierid.SupplierID== item.SupplierID).Add(item);
}

BindingContext = this;

结果还是无法分组

我们需要为组 header 设置模板,试试下面的代码。

 <CollectionView ItemsSource="{Binding SupplierList}"  IsGrouped="true">
        <CollectionView.GroupHeaderTemplate>
            <DataTemplate>
                <Label Text="{Binding SupplierID}" FontAttributes="Bold"/>
            </DataTemplate>
        </CollectionView.GroupHeaderTemplate>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Label Text="{Binding NameProduct}"/>
            </DataTemplate>
        </CollectionView.ItemTemplate>
</CollectionView>