购物车数量没有增加

时间:2017-12-09 13:03:33

标签: xamarin.ios

我们正在c#开发iOS购物车应用程序,为xamarin开发visual studio我们使用listview显示项目和数量文本框,在数量输入框旁边我们放置了两个按钮加号和减号来增加和减少数量。

我们可以在列表视图中看到数量和按钮,但是当我们点击+时,按钮数量没有增加,与-按钮相同。

请求您帮助我们解决此问题,我们将粘贴代码

c# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections.ObjectModel;
using ShopApp.Models;
using System.Reflection;
using System.IO;
using ShopApp.Views;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Windows.Input;

namespace ShopApp.SalesOrderPages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class AllItems : ContentPage
    {
        ObservableCollection<Catalogcls> employees = new ObservableCollection<Catalogcls>();
        public AllItems()
        {  
            InitializeComponent();

            var assembly = typeof(LoadResourceText).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream("ShopApp.Catalog_Master.json");

            Catalogcls[] CatalogObj;

            using (var reader = new StreamReader(stream))
            {
                var json = reader.ReadToEnd();
                var jsondeserilize = JsonConvert.DeserializeObject<RootObject2>(json);
                CatalogObj = jsondeserilize.Catalog;
            }

            for (int i = 0; i < CatalogObj.Length; i++)
            {
                employees.Add(new Catalogcls
                {
                    Image = CatalogObj[i].Image,
                    Name = CatalogObj[i].Name,
                    Pack = CatalogObj[i].Pack,
                    Price = CatalogObj[i].Price,
                    UnitPrice = CatalogObj[i].UnitPrice

                });

                lstView.ItemsSource = employees;

            }
        }

    }
}

Models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopApp.Models
{
    public class Catalogcls
    {
        public string Type { get; set; }
        public string Image { get; set; }
        public string Name { get; set; }
        public double UnitPrice { get; set; }
        public string Price { get; set; }
        public string Pack { get; set; }

    }

    public class RootObject2
    {
        public Catalogcls[] Catalog { get; set; }
    }
}

Xaml code

<?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="ShopApp.SalesOrderPages.AllItems">

    <StackLayout Orientation="Vertical">
        <Label Text="ListView" FontSize="60" ></Label>
        <ListView x:Name="lstView" HasUnevenRows="True" >
            <ListView.Header>
                <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" >
                    <StackLayout Orientation="Vertical" HorizontalOptions="EndAndExpand" >
                        <Label Text="Total:"  FontSize="15" TextColor="Black"  />
                    </StackLayout>
                    <StackLayout Orientation="Vertical" HorizontalOptions="EndAndExpand" Padding="0,0,20,0">
                        <Image Source="cart.png" x:Name="CartImage" Opacity="0.5" AnchorX="0.5" AnchorY="0.5"   HeightRequest="40">
                        </Image>
                        <Button WidthRequest="100" HeightRequest="20"  Text="Nextpage"></Button>
                    </StackLayout>
                </StackLayout>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal" >
                            <Grid Padding="20">
                                <Image Source="{Binding Image}"  WidthRequest="100"  HeightRequest="100"/>
                            </Grid>
                            <StackLayout Orientation="Vertical" Padding="20" >
                                <StackLayout>
                                    <Label Text="{Binding Name}"  FontSize="15" TextColor="Black" />
                                    <Label Text="{Binding Price}"  FontSize="15" TextColor="Black" />
                                    <Label Text="{Binding Pack}"  FontSize="15" TextColor="Black" />
                                    <Label Text="{Binding UnitPrice}"  FontSize="15" TextColor="Black" />
                                </StackLayout>
                                <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="Center">
                                    <StackLayout Orientation="Vertical">
                                        <Button WidthRequest="60" HeightRequest="20" Text="-"  Command="{Binding DecreaseQuantityCommand}" TextColor="Black"></Button>// on click of - button not able bind value for entrybox 
                                    </StackLayout>
                                    <StackLayout Orientation="Vertical" >
                                        <Entry WidthRequest="40" HeightRequest="20"  Text="{Binding Quantity, Mode=TwoWay}" ></Entry>
                                    </StackLayout>
                                    <StackLayout Orientation="Vertical">
                                        <Button WidthRequest="60" HeightRequest="20" Text="+"  Command="{Binding IncreaseQuantityCommand}"  TextColor="Black"  ></Button> 
                                    </StackLayout>
                                </StackLayout>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

0 个答案:

没有答案