Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C无法将项添加到可观察集合_C#_List_Windows Phone_Observablecollection_Items - Fatal编程技术网

C# C无法将项添加到可观察集合

C# C无法将项添加到可观察集合,c#,list,windows-phone,observablecollection,items,C#,List,Windows Phone,Observablecollection,Items,我有一个问题,我试图将我的项目添加到我的可观察集合中,但我得到以下错误: 参数1:无法从“System.Collections.Generic.List”转换为“GameFinder.GetGamesList” 代码: 试一试 gameData是一个列表,因此您需要将gameData中的每个项目添加到项目列表中,并且由于ObservableCollection没有AddRange,您需要在循环中手动执行该操作您到底不了解什么不能从“System.Collections.Generic.List”

我有一个问题,我试图将我的项目添加到我的可观察集合中,但我得到以下错误:

参数1:无法从“System.Collections.Generic.List”转换为“GameFinder.GetGamesList”

代码:

试一试


gameData是一个列表,因此您需要将gameData中的每个项目添加到项目列表中,并且由于ObservableCollection没有AddRange,您需要在循环中手动执行该操作

您到底不了解什么不能从“System.Collections.Generic.List”转换为“GameFinder.GetGamesList”?Add方法接受单个元素,而您传递的是元素列表。您可以使用新的ObservableCollectiongameData。并删除Linq查询末尾的ToList语句。

或查看“谢谢它的工作原理:。我想将oberservable集合项绑定到我的LongListSelector,但它不起作用。在我的xaml中,我这样做:ItemsSource={Binding Items}知道为什么吗?谢谢。假设DataContext很好,我看不出绑定不起作用的原因。我缺少DataContext。我这样使用它:this.DataContext=Items;亲爱的Vasek,欢迎访问SO,请阅读以下链接:我理解错误消息,但我不确定如何解决它。您也可以使用第三方ObservableList库将您的列表包装在ObservableList中。e、 g.var obvList=新的可观察的数据;
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; }
}
foreach (var item in gameData) Items.Add(item)