0

I have a problem where im trying to add my items to my observable collection, but i get this error:

Argument 1: cannot convert from 'System.Collections.Generic.List' to 'GameFinder.GetGamesList'

code:

private void RequestCompleted(object sender, DownloadStringCompletedEventArgs e)
    {

        if (e.Error == null)
        {
            var feedXml = XDocument.Parse(e.Result);

            var gameData = feedXml.Root.Elements("Game").Select(x => new GetGamesList
            {
                ID = (int)x.Element("id"),
                GameTitle = (string)x.Element("GameTitle"),
                ReleaseDate = (string)x.Element("ReleaseDate"),
                Platform = (string)x.Element("Platform")
            })
              .ToList();
            Items.Add(gameData); // THE ERROR IS HERE - Items is the observablecollection
        }
    }

private ObservableCollection<GetGamesList> _Items = new ObservableCollection<GetGamesList>();
    public ObservableCollection<GetGamesList> Items
    {
        get
        {
            return this._Items;
        }
    }

public class GetGamesList
{
    public int ID { get; set; }
    public string GameTitle { get; set; }
    public string ReleaseDate { get; set; }
    public string Platform { get; set; }
}
Thunder
  • 117
  • 18
  • You could also wrap your list in an ObservableList using an 3rd party ObservableList library. ( e.g. var obvList = new ObservableList(gameData); ) https://github.com/gsonnenf/Gstc.Collections.ObservableLists – Greg Jul 22 '19 at 10:13

2 Answers2

1

Try

foreach (var item in gameData) Items.Add(item)

gameData is a List<GetGamesList> so you need to add each item from gameData into Items list and since ObservableCollection doesn't have AddRange you'll need to do it manually in the loop

dkozl
  • 32,814
  • 8
  • 87
  • 89
  • Or see [this](http://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each) – Tomas Pastircak Apr 07 '14 at 13:00
  • Thanks it works :). I would like to bind the oberservable collection Items to my LongListSelector, but its not working. In my xaml i do this: ItemsSource="{Binding Items}" any ideas why? Thanks. – Thunder Apr 07 '14 at 13:40
  • Assuming that `DataContext` is fine I see no reason why that binding would not work – dkozl Apr 07 '14 at 13:49
  • Im missing the DataContext. – Thunder Apr 07 '14 at 13:53
  • i used it like this: this.DataContext = Items; – Thunder Apr 07 '14 at 14:00
  • then change `ItemsSource="{Binding Items}"` to `ItemsSource="{Binding}"` because `DataContext` is already `Items` – dkozl Apr 07 '14 at 14:19
  • Can you tell me why i can't do the same thing with binding on a new page i navigate to on my app. I have another longlistselector on a new page i want to show data in too, but when i navigate to that page it doesn't show anything. Its the same thing i do except i use a new observablecollection for it. Any ideas?. Its a Windows Phone app. – Thunder Apr 08 '14 at 12:34
-2

What exactly don't you understand on cannot convert from 'System.Collections.Generic.List' to 'GameFinder.GetGamesList'? Add method accepts single element, whereas you are passing in a list of elements. You can use new ObservableCollection<GetGamesList>(gameData). And remove the ToList statement at the end of you Linq query.

Vasek
  • 101
  • 2