在视图xamarin中的分类列表中列出模型项

时间:2018-06-23 04:59:12

标签: json xamarin xamarin.forms

使用如下所示的Resturent模型,我正在检索数据以具有两个列的列表的形式显示在列表视图中

模型

public class ResturentItems
    {
        [JsonProperty("Resturent")]
        public List<Resturent> Resturent { get; set; }
    }

    public class Resturent : ModelBase
    {
        [JsonIgnore]
        private static readonly ILocalFileToBase64StringConverter _base64Converter = DependencyService.Get<ILocalFileToBase64StringConverter>();

        [JsonProperty("ID")]
        public string ID { get; set; }

        [JsonProperty("Category")]
        public string Category { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("Code")]
        public string Code { get; set; }

        [JsonProperty("Description")]
        public string Description { get; set; }

        [JsonProperty("Price")]
        public string Price { get; set; }

        [JsonProperty("isservicecharge")]
        public bool isservicecharge { get; set; }

        [JsonProperty("isaddonapplicable")]
        public bool isaddonapplicable { get; set; }

        [JsonProperty("CostPrice")]
        public string CostPrice { get; set; }

        [JsonProperty("Date")]
        public string Date { get; set; }

        [JsonProperty("Image")]
        public string Image { get; set; }

        [JsonIgnore]
        public ImageSource ImageSource => string.IsNullOrEmpty(Image) ? "userpic.jpg" : ImageSource.FromStream(() => new MemoryStream(_base64Converter.ConvertBack(Image)));

    }

,并通过以下代码检索数据

private List<ResturentPair> GetDishItems(List<Resturent> list)
        {         

            var res = new List<ResturentPair>();
            for (int i = 0; i < list.Count-1; i += 2)
            {
                res.Add(new ResturentPair { Item1 = list[i], Item2 = list[i+1] });
            }
            return res;
        }   

为方便起见,我也会在模型下方显示

public class ResturentPair
    {
        public Resturent Item1 { get; set; }

        public Resturent Item2 { get; set; }
    }

因此,最后,餐厅详细信息随名称和类别ETC一起出现,但我的要求我只想显示类别(餐厅项目列表需要列为类别,因此任何想法都只能检索仅按餐厅商品的类别列出??谢谢您的支持

以下有关视图模型的完整代码

public class RootViewModel : ContextViewModelBase
    {
        private readonly IRestService _restService;
        private RootPage _page;

        public RootViewModel(IAppContext context, IRestService restService, IStatusBarService statusBarService) : base(context)
        {
            _restService = restService;
            statusBarService.ShowStatusBar();
            statusBarService.SetDefaultFontColor();
            ActionCommand = new Command(OnActionCommand);
            PageAppearingCommand = new Command<RootPage>(OnPageAppearingCommand);
            PageDisappearingCommand = new Command(OnPageDisappearingCommand);
        }

        public List<ResturentPair> Items { get; set; }

        public List<Resturent> CarouselItems { get; set; }

        public string Photo { get; set; } = "Photo.png";

        public bool IsLoading { get; set; } = true;

        public ICommand ActionCommand { get; }

        public string PagingText { get; set; }

        private async void OnPageAppearingCommand(RootPage page)
        {
            _page = page;
            Photo = $"{DefaultValues.ServerUrl}{Context.User.PhotoUrl}";
            IsLoading = true;
            var items = await _restService.GetFoodMenuInfo();
            CarouselItems = GetCarouselItems(items.Resturent);
            Items = GetDishItems(items.Resturent);
            OnCarouselViewItemSelected(this, new SelectedItemChangedEventArgs(CarouselItems.First())); // we call it for setting paging
            IsLoading = false;
            _page.CarouselView.ItemSelected += OnCarouselViewItemSelected;
        }

        private void OnPageDisappearingCommand()
        {
            _page.CarouselView.ItemSelected -= OnCarouselViewItemSelected;
        }

        private List<Resturent> GetCarouselItems(List<Resturent> items)
        {         

            var carouselViewItems = new List<Resturent>();
            // put your logic here for selecting items for carouselView
            for (var i = 0; i < 4; i++)
            {
                carouselViewItems.Add(items[i]);
            }
            return carouselViewItems;
        }       

        private List<ResturentPair> GetDishItems(List<Resturent> list)
        {         

            var res = new List<ResturentPair>();
            for (int i = 0; i < list.Count-1; i += 2)
            {
                res.Add(new ResturentPair { Item1 = list[i], Item2 = list[i+1] });
            }
            return res;
        }


        private void OnCarouselViewItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var selectedIndex = CarouselItems.IndexOf(e.SelectedItem as Resturent);
            var pagingResult = string.Empty;
            for (var i = 0; i < CarouselItems.Count; i++)
            {
                if (i == selectedIndex)
                {
                    pagingResult += $" \uf26d ";
                }
                else
                {
                    pagingResult += $" \uf26c ";
                }
            }
            PagingText = pagingResult;
        }

        private void OnActionCommand()
        {
            Navigator.PushAsync<SettingsViewModel>();
        }
    }

以下是用于API调用的示例json

{
    "Resturent": [
        {
            "ID": "12238000003927",
            "Category": "LEAF TEA_PRE",
            "Name": "LT 100G COP",
            "Code": "80067-012",
            "Description": "LT 100G COP",
            "Price": "140.000",
            "isservicecharge": 0,
            "isaddonapplicable": 0,
            "CostPrice": ".000",
            "Date": "12228000000000",
            "Image": ""
        },
        {
            "ID": "12238000004015",
            "Category": "LEAF TEA_PRE",
            "Name": "LEAF TEA 200g PREMIUM",
            "Code": "80033-024",
            "Description": "LEAF TEA 200g\/24 PREMIUM (PILLOW PACK)",
            "Price": "310.000",
            "isservicecharge": 0,
            "isaddonapplicable": 0,
            "CostPrice": ".000",
            "Date": "12228000000000",
            "Image": ""
        },
        {
            "ID": "12238000006325",
            "Category": "TEA BAGS_PRE",
            "Name": "PREMIUM TAG TBAG 050 ",
            "Code": "80021-024",
            "Description": "TAG TBAG 050\/2.0g\/24 PREMIUM",
            "Price": "240.000",
            "isservicecharge": 0,
            "isaddonapplicable": 0,
            "CostPrice": ".000",
            "Date": "12228000000000",
            "Image": ""
        },
        {
            "ID": "12238000006192",
            "Category": "TEA BAGS_PRE",
            "Name": "TAG TBAG 025 PREMIUM",
            "Code": "80011-012",
            "Description": "TAG TBAG 025\/2.0g\/12 PREMIUM",
            "Price": "125.000",
            "isservicecharge": 0,
            "isaddonapplicable": 0,
            "CostPrice": ".000",
            "Date": "12228000000000",
            "Image": ""
        },
        {
            "ID": "12238000006703",
            "Category": "LEAF TEA_GOU",
            "Name": "TINCADDY 125g EA",
            "Code": "81032-006",
            "Description": "TIN CADDY 125g\/06 ENGLISH AFTERNOON",
            "Price": "450.000",
            "isservicecharge": 0,
            "isaddonapplicable": 0,
            "CostPrice": ".000",
            "Date": "12228000000001",
            "Image": ""
        },
        {
            "ID": "12238000003727",
            "Category": "TEA BAGS_GOU",
            "Name": "FOIL ENV TBAG 025 ENG BFST",
            "Code": "80086-012",
            "Description": "FOIL ENV TBAG 025\/2.0g\/12 ENGLISH BREAKFAST",
            "Price": "160.000",
            "isservicecharge": 0,
            "isaddonapplicable": 0,
            "CostPrice": ".000",
            "Date": "12228000000001",
            "Image": ""
        }
    ]
}

0 个答案:

没有答案