简体   繁体   English

从 MainPage.xaml.cs 文件到 Xamarin 中的 MainPage.xaml 文件中获取 int 变量的值

[英]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.

相关问题 在Windows Phone中从MainPage.xaml.cs文件到MainPage.xaml文件获取字符串变量的值 - Get value of a string Variable from MainPage.xaml.cs file to MainPage.xaml file in Windows Phone Xamarin 从 MainPage.xaml.cs 看不到 MainPage.xaml 中的对象 - Xamarin can't see objects in MainPage.xaml from MainPage.xaml.cs 将 ToolbarItems 添加到 Xamarin.Forms 项目中的 MainPage.xaml.cs - Adding ToolbarItems to MainPage.xaml.cs in a Xamarin.Forms Project 在 UWP 应用程序中,读取命令行 arguments 并将其从 App.xaml.cs 文件传递到 MainPage.Z44CC44B81911F4BA4581C27 - In UWP App, read command line arguments and pass it from App.xaml.cs file to the MainPage.xaml.cs 类在MainPage.xaml.cs上实例化2次 - Class instantiate 2 times on MainPage.xaml.cs 从phonegap应用程序的MainPage.xaml.cs切换html页面 - Switching the html page from the MainPage.xaml.cs of a phonegap application 如何从MainPage.xaml.cs调用LoadData() - How to call LoadData() from MainPage.xaml.cs 删除 MainPage.xaml - Removing the MainPage.xaml 无法初始化MainPage.xaml.cs中的AudioGraph并无法通过MainPage.xaml.cs上的滑块设置频率 - Unable to initialize an AudioGraph in MainPage.xaml.cs and set frequency with a slider on MainPage.xaml.cs 从Windows Phone 8中的App.Xaml.cs到达MainPage.Xaml.cs单击处理程序函数 - Reaching MainPage.Xaml.cs click handler functions from App.Xaml.cs in windows phone 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM