簡體   English   中英

我該如何解決:名稱“productQuantity”在當前上下文中不存在

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

我在 Visual Studio 中使用 Xamarin.forms。 我遇到的問題是,我在 NuevaVenta.xaml 文件中將條目命名為 x:Name="productQuantity",當我嘗試在 NuevaVenta.xaml.cs 文件中使用該條目時,它說:名稱“productQuantity” ' 不會在當前上下文中退出。 所以無論如何我都不能使用它。

這是我的 .cs 文件:

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());
    }
  }
}

這是我的 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.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>

通常,您無法按名稱訪問項目模板內的任何控件。 如果您嘗試在后面的代碼上訪問此控件,則在 ItemTemplate 中為任何控件提供 x:Name 會給您一個編譯器錯誤,而是在 XAML 中分配 Click 處理程序(或使用命令)。

所以我需要為按鈕點擊創建方法,在你的代碼中,我使用 subButton_Clicked 方法。 然后Object 是 Sender ,它是按鈕單擊事件的處理程序。 接下來我們要通過分析xaml文件找到按鈕的父布局或父容器,最后我們可以訪問父元素的所有子元素。

使用您的代碼做一個示例:

  <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; }
}

這是屏幕截圖:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM