listView xamarin.forms中的句柄按钮



我是新来的xamarin.forms。我不知道如何处理ListView中的按钮。我想用 和 - 函数创建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>

这是我的ListView的图像

我想从API获取数据,客户可以从菜单中选择数字,然后将其发送到服务器。喜欢预订应用。

参考以下代码

在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中实现它。

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

最新更新