【发布时间】:2020-04-22 17:35:00
【问题描述】:
我在 Visual Studio 中使用 Xamarin.forms。我遇到的问题是我不知道如何获取在 xaml 文件的列表视图内的标签上显示的文本,因为我想使用该文本对 xaml 中的另一个列表进行一些更改。 .cs 文件。
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Saansa.Views.CarritoDeVentas">
<ContentPage.Content>
<StackLayout BackgroundColor="Blue">
<ListView x:Name="listaArticulosCarrito" BackgroundColor="White">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<StackLayout HorizontalOptions="StartAndExpand">
<Label Text="{Binding Producto}" Padding="7"
TextColor="Black" FontSize="Large"/>
</StackLayout>
<StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand">
<Label x:Name="PrecioProducto" Text="{Binding Precio}" VerticalOptions="CenterAndExpand"
TextColor="LightGray" HorizontalOptions="EndAndExpand"/>
<Label x:Name="CantidadProducto" Text="{Binding Cantidad}" Padding="7"
TextColor="Black" FontSize="Large"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout BackgroundColor="Aqua" Orientation="Horizontal">
<StackLayout BackgroundColor="Red">
<Label Text="Precio total de la venta:" HorizontalOptions="StartAndExpand"
Padding="7"/>
</StackLayout>
<StackLayout BackgroundColor="Yellow" HorizontalOptions="EndAndExpand">
<Label Text="{Binding price}" Padding="7"/>
</StackLayout>
</StackLayout>
<Button Text="Pagar" BackgroundColor="White" Clicked="Button_Clicked"
BorderColor="Orange" BorderWidth="2" CornerRadius="15" Margin="10"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
我的 xaml 文件以及我想要在 xaml.cs 文件中进行更改的值是名为“PrecioProducto”的标签中的文本。如果你能告诉我代码是如何用 c# 编写的,那真的会对我有所帮助。
【问题讨论】:
-
该标签的文本在您的模型中设置为属性
Precio,因此您可以参考您的模型来获取该值 -
非常感谢,这对我打算做的另一件事很有帮助。
-
你的意思是把listView中的绑定项显示到另一个标签上吗?