我无法访问x:在XAML和代码后面的ListView中的标签控件的名称



我有一个ListView在Xamarin表单有一个Viewcell在它。Viewcell包含一个带有x:Name属性的标签控件。现在我试图在代码后面的x:名称,但它显示编译时错误(x:名称不存在于当前上下文中)。下面的代码使用。

<ListView x:Name="UnPaidInvoicesList"
SeparatorVisibility="None" IsPullToRefreshEnabled="True"
HasUnevenRows="True"                    
ItemSelected="UnPaidInvoicesList_ItemSelected" 
VerticalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout x:Name="stkDuedate" Orientation="Horizontal">
<Label x:Name="lblDueDate1" Text="Due Date:" TextColor="Gray" />
<Label x:Name="lblDueDate"  TextColor="{StaticResource Pink}" />
</StackLayout>

</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>```
Im new to this xamarin forms. Please help on this.

你想实现什么功能?正如Jason所说,模板中的元素不能通过名称来引用。

如果需要显示数据列表,可以将数据绑定到ListView

对此,可以参考以下代码:

<ListView  x:Name="lstView" RowHeight="60"  ItemsSource="{Binding veggies}" HasUnevenRows="True" IsPullToRefreshEnabled="False" SelectionMode="None">
<ListView.ItemTemplate >
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="Fill" BackgroundColor="Olive">
<StackLayout Orientation="Vertical">
<Label Text = "{Binding Name}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/>
<Label Text = "{Binding Type}" AbsoluteLayout.LayoutBounds="50, 35, 200, 25"/>
</StackLayout>
<Image Source="{Binding Image}" HorizontalOptions="End" AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50 "/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

更多信息,您可以查看文档Xamarin。表单ListView和

自定义ListView单元格外观。

您还可以检查样例Xamarin。表单- ListView示例:自定义单元格。

最新更新