[英]How can I fix: The name 'productQuantity' does not exit in the current context
I'm using Xamarin.forms in visual studio.我在 Visual Studio 中使用 Xamarin.forms。 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.我遇到的问题是,我在 NuevaVenta.xaml 文件中将条目命名为 x:Name="productQuantity",当我尝试在 NuevaVenta.xaml.cs 文件中使用该条目时,它说:名称“productQuantity” ' 不会在当前上下文中退出。 So I can't use it in anyway.所以无论如何我都不能使用它。
This is my .cs file:这是我的 .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());
}
}
}
This is my xaml file:这是我的 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>
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.如果您尝试在后面的代码上访问此控件,则在 ItemTemplate 中为任何控件提供 x:Name 会给您一个编译器错误,而是在 XAML 中分配 Click 处理程序(或使用命令)。
So I need to create method for Button click, in your code, I use subButton_Clicked method.所以我需要为按钮点击创建方法,在你的代码中,我使用 subButton_Clicked 方法。 Then Object is Sender which is the handler for button click event.然后Object 是 Sender ,它是按钮单击事件的处理程序。 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.接下来我们要通过分析xaml文件找到按钮的父布局或父容器,最后我们可以访问父元素的所有子元素。
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:这是屏幕截图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.