問題
我對 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="⊖" 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="⊕" 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>
產品類別
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));
}
}
}
類別組類
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內部StackLayout的DataTemplate:
<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預期的型別,奇怪的是除了命令之外的其他系結值都有效,即使智能感知將它們參考到錯誤的型別。
我的兩個問題是:
- How would I go about trying to fix that command
- Can I fix the intellisense so that it recognises when grouping is true (or is this a known issue?)
Thanks in advance!
Solved Thanks @Jason!
For some reason earlier in my code, the CommandParameter didn't want to work as an int and so I had used this long winded method that converted to a string and then back to an int within the Command's lambda method. Turns out I didn't need this and the fix was just to change the parameter type of the Command to int as so:
public ICommand EditProduct
{
get
{
return new Command<int>((param) =>
{
Console.WriteLine("Triggered!");
LoadProduct(param);
});
}
}
uj5u.com熱心網友回復:
ID是一個int,所以你需要使用Command<int>,而不是Command<string>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/439375.html
標籤:xamarin data-binding intellisense collectionview
上一篇:使用回應反序列化Json
下一篇:在iOS中自動暫停本地通知
