从viewmodel绑定listview的itemsource

时间:2018-01-16 13:57:11

标签: c# xaml mvvm xamarin.forms

我的ListView保持为空。 我错过了什么?

近乎所有的编辑: 我在模型中创建了JSON类。 我认为我的JSON有点不对劲。

我希望使用MVVM将存储在我的网络服务器上的json文件解析为listview。现在只有我手动放入的两个项目。

我真的很感激一些帮助。

viewmodel:

using KD_CHKLST.Models;
using KD_CHKLST.Utils;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;

namespace KD_CHKLST.ViewModels
{
    public class AbfahrtskontrolleViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<Abfahrtskontrolle> items;

        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<Abfahrtskontrolle> Items
            {
                get { return items; }
                set
                {
                    items = value;
                }
            }



            public AbfahrtskontrolleViewModel()
            {
            Items = new ObservableCollection<Abfahrtskontrolle>() {
                new Abfahrtskontrolle()
                {
                    text = "Frage 1",
                    description = "Description 1"
                },
                  new Abfahrtskontrolle()
                {
                    text = "Frage 2",
                    description = "Description 2"
                },

            };


            MyHTTP.GetAllNewsAsync(list =>
                 {
                     foreach (Abfahrtskontrolle item in list)
                     {
                         Items.Add(item);
                     }
                 });
            }
        }
    }

模特:

using System.Collections.Generic;

namespace KD_CHKLST.Models
{
    public class Abfahrtskontrolle
    {
        // JSON - Abfahrtskontrollfragen 
        public class Frage
        {
            public string text { get; set; }
            public string description { get; set; }
        }
        public class RootObject
        {
            public IEnumerable<Frage> frage { get; set; }
        }
    }
}

我的json文件:

{
    "frage": [{
            "text": "Frage 1",
            "description": "Frage 1 Description"
        },      
        {
            "text": "Frage 2",
            "description": "Frage 2 Description"
        }
    ]
}

最后是我的http类:

using KD_CHKLST.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;

namespace KD_CHKLST.Utils
{
    public class MyHTTP
    {
        public static async Task GetAllNewsAsync(Action<IEnumerable<Abfahrtskontrolle>> action)
        {

            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = await httpClient.GetAsync("http://xxx.xxx.xxx.xxx/chklst.json");

            Debug.WriteLine(response.Content.ToString());

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var list = JsonConvert.DeserializeObject<IEnumerable<Abfahrtskontrolle>>(await response.Content.ReadAsStringAsync());
                Debug.WriteLine(" hier ist meine geparste Liste: " + list);
                action(list);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

必须更改我的模型,我的json并将propertychangedevent添加到我的Items中。现在它正在运作!

我改变了模型:

namespace KD_CHKLST.Models
{
    public class Abfahrtskontrolle
    {
        public int FrageID { get; set; }
        public string Text { get; set; }
        public string Description { get; set; }
    }
}

我在ViewModel中更改了ObservableCollection: Items

public ObservableCollection<Abfahrtskontrolle> Items
            {
                get { return items; }
                set
                {
                    items = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Items)));
                }
            }

现在可以使用,因为我编辑了我的JSON:

[
  {
    "FrageID": 0,
    "Text": "text 1",
    "Description": "description 1"
  },
  {
    "FrageID": 1,
    "Text": "text 2",
    "Description": "description 2"
  }
]

现在它正在工作,我终于设法通过MVVM绑定列表视图中的数据。 感谢您的支持。

-p

相关问题