[英]Get value of an int Variable from MainPage.xaml.cs file to MainPage.xaml file in Xamarin
I have created a project in which the MainPage.xaml.cs is called: CarritoDeVentas.xaml.cs and is like the following:我创建了一个项目,其中 MainPage.xaml.cs 被称为:CarritoDeVentas.xaml.cs,如下所示:
using System;
using System.Collections.Generic;
using System.Collections;
using SQLite;
using Xamarin.Forms;
using Saansa.Modelos;
namespace Saansa.Views
{
public partial class CarritoDeVentas : ContentPage
{
public string strQR;
public int price { set; get; }
public CarritoDeVentas()
{
InitializeComponent();
listaArticulosCarrito.ItemsSource = App.listaCarrito;
price = 0;
this.strQR = "";
foreach (Modelos.ArticuloCarrito a in App.listaCarrito) {
price += a.Precio;
}
}
}
And my CarritoDeVentas.xaml is like the following:我的 CarritoDeVentas.xaml 如下所示:
<?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="White">
<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="CantidadProducto" Text="{Binding Cantidad}" Padding="7"
TextColor="LightGray" FontSize="Large"/>
<Label x:Name="PrecioProducto" Text="{Binding Precio}" VerticalOptions="CenterAndExpand"
TextColor="Black" HorizontalOptions="EndAndExpand" FontSize="Large" Padding="7"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout Orientation="Horizontal">
<StackLayout>
<Label Text="Precio total de la venta:" HorizontalOptions="StartAndExpand"
Padding="7" Margin="10,0,10,0" FontSize="Medium"/>
</StackLayout>
<StackLayout HorizontalOptions="EndAndExpand">
<Label Text="{Binding price}" Padding="7"/>
</StackLayout>
</StackLayout>
<Button Text="Pagar" BackgroundColor="#673AB7" Clicked="Button_Clicked"
CornerRadius="15" Margin="10"/>
<Button x:Name="QRbut" Text="Generar CodigoQR" BackgroundColor="#26A69A" Clicked="generateQR_Clicked"
CornerRadius="15" Margin="10,0,10,10"/>
</StackLayout>
</ContentPage.Content>
My question is: How can I show the int price in the xaml.cs file in the xaml file on a label?我的问题是:如何在 label 上的 xaml 文件中的 xaml.cs 文件中显示 int 价格? What I have tried is to try a binding, but I'm not sure if I did it righ, im new at this.我试过的是尝试绑定,但我不确定我是否做得对,我是新手。
Option 1 - Direct assignment选项 1 - 直接分配
// XAML
<Label x:Name="MyPrice" Padding="7"/>
// code-behind
MyPrice.Text = price.ToString();
Option 2 - binding选项 2 - 绑定
// XAML
<Label Text="{Binding price}" Padding="7"/>
// code behind
// you must set the BindingContext for binding to work
this.BindingContext = this;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.