C# 如何修复:名称';产品数量';不在当前上下文中退出
我正在visual studio中使用Xamarin.forms。我遇到的问题是,我在NuevaVenta.xaml文件中将一个条目命名为x:Name=“productQuantity”,当我尝试在NuevaVenta.xaml.cs文件中使用该条目时,它会说:“productQuantity”名称在当前上下文中不存在。所以我无论如何也不能用它 这是我的.cs文件:C# 如何修复:名称';产品数量';不在当前上下文中退出,c#,visual-studio,xamarin,xamarin.forms,C#,Visual Studio,Xamarin,Xamarin.forms,我正在visual studio中使用Xamarin.forms。我遇到的问题是,我在NuevaVenta.xaml文件中将一个条目命名为x:Name=“productQuantity”,当我尝试在NuevaVenta.xaml.cs文件中使用该条目时,它会说:“productQuantity”名称在当前上下文中不存在。所以我无论如何也不能用它 这是我的.cs文件: using System; using System.Collection.Generic; using Xamarinin.Fo
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中指定单击处理程序(或使用命令) 所以我需要为按钮点击创建方法,在您的代码中,我使用子按钮点击方法。 然后对象是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; }
}
公共部分类第7页:内容页
{
公共可观测集合乘积{get;set;}
公共页7()
{
初始化组件();
products=新的ObservableCollection()
{
新的Productmodel(){Producto=“product 1”},
新的Productmodel(){Producto=“product 2”},
新的Productmodel(){Producto=“product 3”},
新的Productmodel(){Producto=“product 4”},
新的Productmodel(){Producto=“product 5”},
新的Productmodel(){Producto=“product 6”}
};
this.BindingContext=this;
}
已单击私有void goToCart_(对象发送者,事件参数e)
{
}
私有void addButton_已单击(对象发送者,事件参数e)
{
}
私有void addCart_已单击(对象发送者,事件参数e)
{
}
已单击专用异步无效子按钮(对象发送方,事件参数e)
{
var buttonClickHandler=(按钮)发送方;
StackLayout parentstacklayout=(StackLayout)按钮ClickHandler.Parent;
StackLayout stacklayout1=(StackLayout)parentstacklayout.Children[2];
分录productQuantity=(分录)stacklayout1.子项[0];
等待显示警报(“productQuantity详细信息”,“productQuantity文本为”+productQuantity.text,“OK”);
}
}
公共类产品模型
{
公共字符串Producto{get;set;}
}
这是屏幕截图:
请重新打开我的问题我真的需要解决这个问题,我正在尝试做一个项目,我已经三天没有这个问题了。我已经更改了问题的描述,以便更清楚。我没有看到任何地方您“将条目命名为x:Name=“productQuantity”。有一个条目元素名为
cantidadProducto
,但没有一个条目元素名为productQuantity
。很抱歉,这是我的错误,现在您可以看到名为productQuantity的条目。感谢您的回答,我现在完全理解它为什么不起作用,但我仍然不完全理解如何解决它,抱歉,我是一个新手,如果你能尝试指定更多,那对我来说将是令人惊讶的。谢谢。我已经决定了我要做的事,谢谢。但如果你能再多解释一点,我会非常感激。@AndresChaves你可以把它看作是在Listview模板中寻找一个特定的控件,当你点击按钮时,你可以在这个模板中得到这个按钮,所以你可以通过按钮的父布局得到其他控件。