Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm developing a Windows 8 app and I'm fairly new to the MVVM and I'm stuck at setting up the MainViewModel. Here's my code so far:

My DataItem model class
C#
public class DataItem : ObservableObject
{
  public const string AssetIDPropertyName = "AssetID";      
  public const string TitlePropertyName = "Title";
  public const string DescriptionPropertyName = "Description";
  public const string ImagePropertyName = "Image";
  private string assetID;
  private string title;
  private string description;
  private string image;

  public string AssetID
  {
    get { return assetID; }
    set { Set(AssetIDPropertyName, ref assetID, value); }
  }

  public string Title
  {
    get { return title; }
    set { Set(TitlePropertyName, ref title, value); }
  }      

  public string Description
  {
    get { return description; }
    set { Set(DescriptionPropertyName, ref description, value); }
  }

  public string Image
  {
    get { return image; }
    set { Set(ImagePropertyName, ref image, value); }
  }
}


My DataGroup model class
C#
public class DataGroup : ObservableObject
{
  public const string TitlePropertyName = "Title";
  public const string ImagePropertyName = "Image";
  public const string ItemsPropertyName = "Items";

  private string title;
  private string image;
  private ObservableCollection<DataItem> items;

  public string Title
  {
    get { return title; }
    set { Set(TitlePropertyName, ref title, value); }
  }

  public string Image
  {
    get { return image; }
    set { Set(ImagePropertyName, ref image, value); }
  }

  public ObservableCollection<DataItem> Items
  {
    get { return items; }
    set { Set(ItemsPropertyName, ref items, value); }
  }

  public IEnumerable<DataItem> TopItems
  {
    get { return items.Take(6); }
  }

  public int VideoCount
  {
    get { return Items.Count; }
  }
}


My DataService class
C#
public class DataSource : IDataSource
{
  private string baseUri = "http://www.mywebservice.com/W8/";
  private ObservableCollection<DataGroup> groups;

  public ObservableCollection<DataGroup> Groups
  {
    get { return groups; }
  }

  public async Task GetVideosAsync()
  {
    Task<DataGroup> featured = GetVideoAsync("featured");
    Task<DataGroup> popular = GetVideoAsync("popular");
    Task<DataGroup> recent = GetVideoAsync("recent");

    Groups.Add(await featured);
    Groups.Add(await popular);
    Groups.Add(await recent);
  }

  private async Task<DataGroup> GetVideoAsync(string groupType)
  {
    string serviceUri = string.Format("{0}/viewservice.php?listtype={1}", baseUri, groupType);
    HttpClient client = new HttpClient();

    // Read up to 1MB of data
    client.MaxResponseContentBufferSize = 1024 * 1024;

    HttpResponseMessage response = await client.GetAsync(serviceUri);

    if (response.IsSuccessStatusCode)
    {
      DataGroup group = new DataGroup();
      
      switch (groupType)
      {
        case "featured":
          group.Title = "Featured Content";
          group.Image = "Assets/Images/Kratos.jpg";
          break;
        case "popular":
          group.Title = "Most Popular";
          group.Image = "Assets/Images/Ratchet.jpg";
          break;
        case "recent":
          group.Title = "Most Recent";
          group.Image = "Assets/Images/Assassins.jpg";
          break;
      }

      var result = await response.Content.ReadAsStringAsync();
      var items = JsonArray.Parse(result);

      foreach (var item in items)
      {
        DataItem dataItem = new DataItem();

        dataItem.AssetID = item.GetObject()["assetid"] != null ? item.GetObject()["assetid"].GetString() : string.Empty;
        dataItem.Title = item.GetObject()["title"] != null ? item.GetObject()["title"].GetString() : string.Empty;
        dataItem.Description = item.GetObject()["description"] != null ? item.GetObject()["description"].GetString() : string.Empty;
        dataItem.Image = string.Format("Assets/thumbnails/large/{0}.png", item.GetObject()["assetid"].GetString());          

        group.Items.Add(dataItem);
      }

      return group;
    }

    return null;
  }
}


Right now I'm not sure how to proceed so that I can bind the data to a CollectionViewSource. This is what I've done so far but I get NullReferenceException error. Can someone point me to the right direction?
C#
public class MainViewModel : ViewModelBase
{
  private ObservableCollection<DataGroup> videos;
  private IDataSource dataSource;

  public MainViewModel(IDataSource dataSource)
  {
    this.dataSource = dataSource;

    GetVideos(dataSource);
  }

  public ObservableCollection<DataGroup> Videos 
  {
    get { return videos; }
    set { Set("Videos", ref videos, value); } 
  }

  private async void GetVideos(IDataSource dataSource)
  {
    await dataSource.GetVideosAsync(); //NullReferenceException error here
  }
}
Posted
Updated 10-Mar-14 6:57am
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900