Binding collection from XML in Windows Store async
In my app.xaml.cs I am creating XML using this code:
private async static void CreateXML()
{
StorageFolder sf = await
ApplicationData.Current.LocalFolder.CreateFolderAsync("Data",
CreationCollisionOption.OpenIfExists);
StorageFile st = await sf.CreateFileAsync("Badgess.xml",
CreationCollisionOption.OpenIfExists);
XmlDocument dom = new XmlDocument();
XmlElement x;
x = dom.CreateElement("badges");
dom.AppendChild(x);
XmlElement x1 = dom.CreateElement("badge");
XmlElement x11 = dom.CreateElement("id");
x11.InnerText = "1";
x1.AppendChild(x11);
XmlElement x12 = dom.CreateElement("name");
x12.InnerText = "Badge One";
x1.AppendChild(x12);
XmlElement x13 = dom.CreateElement("pictureurl");
x13.InnerText = "two.png";
x1.AppendChild(x13);
XmlElement x14 = dom.CreateElement("isachieved");
x14.InnerText = "false";
x1.AppendChild(x14);
x.AppendChild(x1);
await dom.SaveToFileAsync(st);
}
When the app is started I can't see this data, I am using repository
pattern and bind object to observableList like this:
public async Task<IList<Badge>> GetAll()
{
StorageFolder sf = await
ApplicationData.Current.LocalFolder.GetFolderAsync("Data");
StorageFile st = await sf.GetFileAsync("Badgess.xml");
XDocument doc = XDocument.Parse(await FileIO.ReadTextAsync(st));
var data = from query in doc.Descendants("badge")
select new Badge
{
Id = (int)query.Element("id"),
Name = (string)query.Element("name"),
PictureURL = (string)query.Element("pictureurl"),
IsAchieved = (bool)query.Element("isachieved"),
};
return (IList<Badge>)data.ToList();
}
Binding:
IList<Badge> list = await _badgeService.GetAll();
BadgesList = list.ToObservableCollection<Badge>();
It appears only when I go to the another view of the app and then go back
to the main view, it is not appear after application is starting and I do
not know why? Will be great if some could help me because I am fighting
with this for 2 days and have no idea, the project is hosted on GitHub, so
you can take a look if you need this.
No comments:
Post a Comment