【问题标题】:Handle button in listview Xamarin.Forms列表视图 Xamarin.Forms 中的句柄按钮
【发布时间】:2019-03-04 07:07:32
【问题描述】:

我是 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>

Here is image of my listview

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

【问题讨论】:

  • 嘿,问题解决了吗?
  • 对不起先生,我遇到了一些新问题,所以我没有尝试,当我尝试时,我会告诉你结果,非常感谢你的帮助。
  • 是的,稍后我会告诉你

标签: listview xamarin button xamarin.forms


【解决方案1】:

参考以下代码

在xml中

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

在自定义视单元中

在xml中

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

【讨论】:

  • 列表视图中的项目是否可用?
  • 是的,你可以在 viewcell 中调用它们
  • 现在的问题是我不能在 xaml.cs 中调用 edt_quantity
  • 你能分享你的样品吗?
  • 已编辑并添加图片,请帮助我
猜你喜欢
  • 1970-01-01
  • 2016-05-15
  • 2016-12-31
  • 2019-02-03
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 2018-11-21
相关资源
最近更新 更多