我是xamarin.forms的新手。我不知道如何处理列表视图中的按钮。我想用+和-功能创建2个按钮。该条目将默认为0。当我单击+按钮时,该条目将为++,而当我单击-按钮时将为-。任何人都可以告诉我该怎么办。 这是我的代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="QRScanner.Menu">
<ListView x:Name="MyListView"
ItemsSource="{Binding Items}"
ItemTapped="Handle_ItemTapped"
CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell
Height="100"
>
<AbsoluteLayout>
<StackLayout
AbsoluteLayout.LayoutBounds="0,0,0.65,1"
AbsoluteLayout.LayoutFlags="All"
Orientation="Horizontal">
<Image
HorizontalOptions="StartAndExpand"
WidthRequest="70"
HeightRequest="70"
VerticalOptions="CenterAndExpand"
Source="{Binding imgUrl}"
/>
<Label Text="{Binding name}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
/>
<Label
HorizontalOptions="EndAndExpand"
Text="{Binding price}"
VerticalOptions="CenterAndExpand"/>
</StackLayout>
<StackLayout
AbsoluteLayout.LayoutBounds="1,0,0.35,1"
AbsoluteLayout.LayoutFlags="All"
Orientation="Horizontal"
>
<Button
VerticalOptions="Center"
HeightRequest="30"
WidthRequest="30"
Clicked="Button_Clicked"
Text="-"
x:Name="btnMinus"
FontSize="12"
BackgroundColor="White"
TextColor="Green"
BorderColor="Green"/>
<Entry
Keyboard="Numberic"
x:Name="edt_quantity"
Text="{Binding quantity}"
FontSize="12"/>
<Button
x:Name="btnAdd"
VerticalOptions="Center"
WidthRequest="30"
HeightRequest="30"
Clicked="Button_Clicked_1"
Text="+"
FontSize="12"
BackgroundColor="White"
TextColor="Green"
BorderColor="Green"
/>
</StackLayout>
</AbsoluteLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我想从api获取数据,客户可以从菜单中选择数字,然后将其发送到服务器。喜欢预订应用程序。
答案 0 :(得分:0)
请参考以下代码
在xaml中
<StackLayout
Orientation="Horizontal"
HorizontalOptions="EndAndExpand"
VerticalOptions="Center">
<Button
HeightRequest="40"
WidthRequest="40"
Clicked="ClickMinus"
Text="-"
BackgroundColor="White"
TextColor="Green"
BorderColor="Green"/>
<Entry
x:Name="edt_quantity"
Text="0"/>
<Button
WidthRequest="40"
HeightRequest="40"
Clicked="ClickPlus"
Text="+"
BackgroundColor="White"
TextColor="Green"
BorderColor="Green"/>
</StackLayout>
在xaml.cs
private void ClickPlus(object sender, EventArgs e)
{
int num = int.Parse(edt_quantity.Text)+1;
edt_quantity.Text = num.ToString();
}
private void ClickMinus(object sender, EventArgs e)
{
int num = int.Parse(edt_quantity.Text)-1;
edt_quantity.Text = num.ToString();
}
如果要在viewcell中实现它。可以创建viewcell的子类。
<ListView x:Name="MyListView"
CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate>
<local:MyViewCell Height="100" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在“自定义”视图单元格中
在xaml中
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App6.ViewCell1">
<ViewCell.View>
<AbsoluteLayout>
//...
<StackLayout
AbsoluteLayout.LayoutBounds="1,0,0.35,1"
AbsoluteLayout.LayoutFlags="All"
Orientation="Horizontal"
>
<Button
VerticalOptions="Center"
HeightRequest="30"
WidthRequest="30"
Clicked="BtnMinus_Clicked"
Text="-"
x:Name="btnMinus"
FontSize="12"
BackgroundColor="White"
TextColor="Green"
BorderColor="Green"/>
<Entry
Keyboard="Numberic"
x:Name="myEntry"
Text="0"
FontSize="12"/>
<Button
x:Name="btnAdd"
VerticalOptions="Center"
WidthRequest="30"
HeightRequest="30"
Clicked="BtnAdd_Clicked"
Text="+"
FontSize="12"
BackgroundColor="White"
TextColor="Green"
BorderColor="Green"
/>
</StackLayout>
</AbsoluteLayout>
</ViewCell.View>
</ViewCell>
在xaml.cs
public partial class MyViewCell: ViewCell
{
public MyViewCell()
{
InitializeComponent ();
}
private void BtnMinus_Clicked(object sender, EventArgs e)
{
int num = int.Parse(myEntry.Text) - 1;
myEntry.Text = num.ToString();
}
private void BtnAdd_Clicked(object sender, EventArgs e)
{
int num = int.Parse(myEntry.Text) + 1;
myEntry.Text = num.ToString();
}
}