简体   繁体   中英

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:

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:

<?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? 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

// XAML
<Label x:Name="MyPrice" Padding="7"/>

// code-behind
MyPrice.Text = price.ToString();

Option 2 - binding

// XAML
<Label Text="{Binding price}" Padding="7"/>

// code behind
// you must set the BindingContext for binding to work
this.BindingContext = this;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM