我有哪裏我嘗試我的項目添加到我的觀察到的集合問題,但我得到這個錯誤:C#無法將項目添加到觀察的集合
參數1:無法從「System.Collections.Generic轉換.LIST」到 'GameFinder.GetGamesList'
代碼:
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; }
}
或者請參閱[this](http://stackoverflow.com/questions/670577/observablecollection-doesnt-support-addrange-method-so-i-get-notified-for-each) –
謝謝它的作品:)。我想將oberservable集合Items綁定到我的LongListSelector,但它不起作用。在我的xaml中,我這樣做:ItemsSource =「{Binding Items}」有什麼想法爲什麼?謝謝。 – Thunder
假設'DataContext'沒問題,我沒有看到爲什麼綁定不起作用的原因 – dkozl