简体   繁体   中英

How can I fix: The name 'productQuantity' does not exit in the current context

I'm using Xamarin.forms in visual studio. The problem that I have is that I named an Entry as x:Name="productQuantity" in my NuevaVenta.xaml file and when I'm trying to use that Entry in my NuevaVenta.xaml.cs file it says: The name 'productQuantity' does not exit in the current context. So I can't use it in anyway.

This is my .cs file:

using System;
using System.Collection.Generic;
using Xamarinin.Forms;

namespace Saansa.Views{
public partial class NuevaVenta : ContentPage
{
    public Venta()
    {
         InitializeComponent();
    }

    protected async override void OnAppearing()
    {
        base.OnAppearing();
        var articuloLista = await App.SQLiteDb.GetItemsAsync();
        if (articuloLista != null)
        {
            listART.ItemsSource = articuloLista;
        }
    }

    int pQuantity = 0;

    void subButton_Clicked(System.Object sender, System.EventArgs e)
    {
        pQuantity--;
        if (pQuantity == -1) {
            pQuantity = 0;
        }
        productQuantity.Text = pQuantity.ToString();
    }

    void addButton_Clicked(System.Object sender, System.EventArgs e)
    {
        pQuantity++;
        productQuantity.Text = pQuantity.ToString();
    }

    void addCart_Clicked(System.Object sender, System.EventArgs e)
    {
    }

    void goToCart_Clicked(System.Object sender, System.EventArgs e)
    {
        Navigation.PushAsync(new CarritoDeVentas());
    }
  }
}

This is my xaml file:

<?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.NuevaVenta"
         xmlns:local= "clr-namespace:Saansa">
<ContentPage.Content>
    <StackLayout BackgroundColor="#f5cda2">
        <ListView x:Name="listART" BackgroundColor="#f5cda2">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <StackLayout. HorizontalOptions="StartAndExpand">
                                <Label Text="{Binding Producto}"
                                       Margin="5,0,0,0"
                                       FontSize="Large"/>
                            </StackLayout>
                            <Button x:Name="subButton"
                                    Text="-"
                                    BackgroundColor="#b27b4b"
                                    Margin="5,5,0,5"
                                    Clicked="subButton_Clicked"
                                    FontSize="Small"
                                    TextColor="Black"
                                    WidthRequest="30"/>
                            <StackLayout>
                                <Entry Text="0" x:Name="productQuantity"
                                       Placeholder="0" MaxLength="2"
                                       Margin="5,0,0,0" Keyboard="Numeric"
                                       FontSize="Small"
                                       HorizontalOptions="Center"/>
                            </StackLayout>
                            <Button x:Name="addButton"
                                    Text="+"
                                    BackgroundColor="#b27b4b"
                                    Margin="5,5,0,5"
                                    Clicked="addButton_Clicked"
                                    FontSize="Small"
                                    TextColor="Black"
                                    WidthRequest="30"/>
                            <Button x:Name="addCart"
                                    Text="Agregar"
                                    BackgroundColor="#b27b4b"
                                    Margin="5,3,5,3"
                                    Clicked="addCart_Clicked"
                                    FontSize="Small"
                                    TextColor="Black"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <StackLayout VerticalOptions="EndAndExpand">
            <Button x:Name="goToCart" Text="Ir al carrito" BackgroundColor="White" Clicked="goToCart_Clicked"
                     CornerRadius="5" Margin="1"/>
        </StackLayout>
    </StackLayout>
</ContentPage.Content>

Generally, you can't access any control inside the item template by name. giving any control inside ItemTemplate an x:Name will give you a compiler error if you tried to access this control on code behind, Instead assign the Click handler (or use a Command) in the XAML.

So I need to create method for Button click, in your code, I use subButton_Clicked method. Then Object is Sender which is the handler for button click event. Next we have to find the parent layout or parent container for button by analyzing the xaml file, finally we can access all child element of parent element.

Using your code to do one sample:

  <StackLayout>
        <ListView
            x:Name="listART"
            BackgroundColor="#f5cda2"
            HasUnevenRows="True"
            ItemsSource="{Binding products}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <StackLayout HorizontalOptions="FillAndExpand">
                                <Label
                                    Margin="5,0,0,0"
                                    FontSize="Large"
                                    Text="{Binding Producto}" />

                                <Button
                                    x:Name="subButton"
                                    Margin="5,5,0,5"
                                    BackgroundColor="#b27b4b"
                                    Clicked="subButton_Clicked"
                                    FontSize="Small"
                                    Text="-"
                                    TextColor="Black"
                                    WidthRequest="30" />
                                <StackLayout>
                                    <Entry
                                        x:Name="productQuantity"
                                        Margin="5,0,0,0"
                                        FontSize="Small"
                                        HorizontalOptions="Center"
                                        Keyboard="Numeric"
                                        MaxLength="2"
                                        Placeholder="0"
                                        Text="0" />
                                </StackLayout>
                                <Button
                                    x:Name="addButton"
                                    Margin="5,5,0,5"
                                    BackgroundColor="#b27b4b"
                                    Clicked="addButton_Clicked"
                                    FontSize="Small"
                                    Text="+"
                                    TextColor="Black"
                                    WidthRequest="30" />
                                <Button
                                    x:Name="addCart"
                                    Margin="5,3,5,3"
                                    BackgroundColor="#b27b4b"
                                    Clicked="addCart_Clicked"
                                    FontSize="Small"
                                    Text="Agregar"
                                    TextColor="Black" />
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <StackLayout VerticalOptions="EndAndExpand">
            <Button
                x:Name="goToCart"
                Margin="1"
                BackgroundColor="White"
                Clicked="goToCart_Clicked"
                CornerRadius="5"
                Text="Ir al carrito" />
        </StackLayout>
    </StackLayout>

  public partial class Page7 : ContentPage
{
    public ObservableCollection<Productmodel> products { get; set; }
    public Page7()
    {
        InitializeComponent();

        products = new ObservableCollection<Productmodel>()
        {
            new Productmodel(){Producto="product 1"},
            new Productmodel(){Producto="product 2"},
            new Productmodel(){Producto="product 3"},
            new Productmodel(){Producto="product 4"},
            new Productmodel(){Producto="product 5"},
            new Productmodel(){Producto="product 6"}
        };
        this.BindingContext = this;
    }

    private void goToCart_Clicked(object sender, EventArgs e)
    {

    }

    private void addButton_Clicked(object sender, EventArgs e)
    {

    }

    private void addCart_Clicked(object sender, EventArgs e)
    {

    }

    private async void subButton_Clicked(object sender, EventArgs e)
    {
        var buttonClickHandler = (Button)sender;
        StackLayout parentstacklayout = (StackLayout)buttonClickHandler.Parent;
        StackLayout stacklayout1 =(StackLayout)parentstacklayout.Children[2];
        Entry productQuantity = (Entry)stacklayout1.Children[0];
        await DisplayAlert("productQuantity detail","the productQuantity text is "+productQuantity.Text,"OK");
    }
}

public class Productmodel
{
    public string Producto { get; set; }
}

This is the screenshot:

在此处输入图片说明

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