[英]How to bind image in Gridview using xml parsing in windows metro app
I'm working on my first Windows 8 app. 我正在开发我的第一个Windows 8应用程序。 I'm trying to display a GridView populated with Image and Image description.
我正在尝试显示填充有Image和Image描述的GridView。 I want to get my data from an XML file I created.
我想从我创建的XML文件中获取数据。 I found the ItemSource property of the GridView and I try to bind my XML file to it but I can't do this.
我找到了GridView的ItemSource属性,并尝试将XML文件绑定到该属性,但是我不能这样做。
Please tell me right way to do this task. 请告诉我执行此任务的正确方法。 thanx
谢谢
You can't bind an XML file directly to GridView.ItemsSource
, you need to parse it first into an object. 您不能将XML文件直接绑定到
GridView.ItemsSource
,您需要首先将其解析为一个对象。 I'd create a class with all data to be displayed in GridView
: 我将创建一个类,其中所有数据都将显示在
GridView
:
public class GridViewItem
{
public string Description { get; set; }
public ImageSource Image { get; set; }
}
The next step is to parse the XML file into a list of GridViewItem
s: 下一步是将XML文件解析为
GridViewItem
的列表:
var xmlString = await FileIO.ReadTextAsync(storageFile);
var xml = XDocument.Parse(xmlString);
var Items = xml.Element("rootNode").Elements("itemNode").Select(i => new GridViewItem
{
Description = (string)i.Element("descriptionNode"),
Image = ParseImage(i.Element("imageNode"))
}).ToList();
I've assumed the tags in your XML are rootNode
, itemNode
, descriptionNode
and imageNode
. 我认为在你的XML标签是
rootNode
, itemNode
, descriptionNode
和imageNode
。 Also I don't know how your image data is stored in XML. 另外我也不知道您的图像数据如何以XML存储。 The logic to convert it to an
ImageSource
should be in ParseImage()
. 将其转换为
ImageSource
的逻辑应该在ParseImage()
。
The only thing left is to assign the Items
list above to a property in your view model serving as you DataContext
and bind it to ItemsSource
: 剩下的唯一事情就是将上面的
Items
列表分配给充当DataContext
视图模型中的一个属性,并将其绑定到ItemsSource
:
<GridView ItemsSource="{Binding Items}" />
This is the basic idea. 这是基本思想。 There's a lot of details missing in my answer but that's best I can do based on your question.
我的答案中遗漏了许多细节,但是根据您的问题,我最好能做到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.