我四处搜索,但我认为我找不到问题的答案。我是 xamarin 的新手,所以我希望我使用的是正确的术语。我正在尝试在列表视图中使用自定义单元格。我的目标是在我的应用程序的多个部分重用自定义单元格,但是当我使用事件“ItemSelected”时,它会返回到自定义单元格的绑定,而不是我原来的列表视图 itemsource 绑定。我明白我为什么这么想,但我不确定如何将 ItemSelected 绑定到原始源。我在这里使用正确的方法吗?老实说,我完全迷失了。
这是我的自定义单元格代码:
public partial class ListCell : ViewCell
{
public static readonly BindableProperty LabelHeaderProperty = BindableProperty.Create("LabelHeader",typeof(string),typeof(ListCell));
public string LabelHeader
{
get { return (string)Getvalue(LabelHeaderProperty); }
set { Setvalue(LabelHeaderProperty,value); }
}
public static readonly BindableProperty LabelSmallProperty = BindableProperty.Create("LabelSmall",typeof(ListCell));
public string LabelSmall
{
get { return (string)Getvalue(LabelSmallProperty); }
set { Setvalue(LabelSmallProperty,value); }
}
public ListCell()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
BindingContext = new
{
LabelHeader = this.LabelHeader,LabelSmall = this.LabelSmall
};
}
}
这是我的列表视图
<ListView x:Name="MyListView"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
IsPullToRefreshEnabled="true"
ItemSelected="OnItemSelected"
SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<extensions:ListCell LabelHeader="{Binding Description}"
LabelSmall="{Binding Description}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
非常感谢您 :)