This Post is about binding data in to default Windows Store application template that provided in Visual Studio 2012 onwards. ( My demo is with windows 8.1 App in Visual Studio 2013 )
Lets begin :)
1. Open Your Visual Studio and Create
New Project (make sure that you are selecting
Visual c# and
Windows Store )
2. Then add Name and set the path for project and select
Grid App (XAML) template . Then create the solution
3. Then I'm creating separate folder to keep my
XML ( **not compulsory )
R click on Project-> Add ->New Folder , I name it as
xml_data4. R click on the
xml_data folder and
Add-> New Item -> in Data sub menu select
XML File , Named it as
data.xmlI'm gonna create App that have two groups. here is my XML file format(* you can have your own format)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<data>
<item>
<cat>Category 1</cat>
<id>1</id>
<name>Name 1</name>
<image>sample.png</image>
<description>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</description>
</item>
<item>
<cat>Category 1</cat>
<id>2</id>
<name>Name 2</name>
<image>sample.png</image>
<description>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</description>
</item>
<item>
<cat>Category 1</cat>
<id>3</id>
<name>Name 3</name>
<image>sample.png</image>
<description>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</description>
</item>
<item>
<cat>Category 2</cat>
<id>4</id>
<name>Name 4</name>
<image>sample.png</image>
<description>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</description>
</item>
<item>
<cat>Category 2</cat>
<id>5</id>
<name>Name 5</name>
<image>sample.png</image>
<description>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</description>
</item>
<item>
<cat>Category 2</cat>
<id>6</id>
<name>Name 6</name>
<image>sample.png</image>
<description>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</description>
</item>
</data>
5. Then go to the
Data Model folder and select
SampleDataSource.cs and go to the
GetSampleDataAsync() method and format it as below
Code:
private async Task GetSampleDataAsync()
{
if (this._groups.Count != 0)
return;
var group1 = new SampleDataGroup("Group 1",
"Category 1",
"Sample sub 1",
"Assets/DarkGray.png",
"Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
var group2 = new SampleDataGroup("Group 2",
"Category 2",
"Sample sub 2",
"Assets/DarkGray.png",
"Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
Uri dataUri = new Uri("ms-appx:///xml_data/data.xml");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
string xmlString = await FileIO.ReadTextAsync(file);
XDocument xmlDoc = XDocument.Parse(xmlString);
var items = from data in xmlDoc.Descendants("item")
select new
{
Cat_ = data.Element("cat").Value,
Id_ = data.Element("id").Value,
Name_ = data.Element("name").Value,
Img_ = "Assets/" + data.Element("image").Value,
Description_ = data.Element("description").Value,
};
foreach (var itemData in items)
{
if(itemData.Cat_.Equals("Category 1"))
{
group1.Items.Add(new SampleDataItem(itemData.Id_,
itemData.Name_,
"Item Subtitle:",
itemData.Img_,
itemData.Description_,
itemData.Description_));
}
if (itemData.Cat_.Equals("Category 2"))
{
group2.Items.Add(new SampleDataItem(itemData.Id_,
itemData.Name_,
"Item Subtitle:",
itemData.Img_,
itemData.Description_,
itemData.Description_));
}
}
this.Groups.Add(group1);
this.Groups.Add(group2);
}
Then run the code. :) we are done
Full Code In Media Fire http://bit.ly/1bgNdSD