【问题标题】:windows phone 7 xml response handlingwindows phone 7 xml响应处理
【发布时间】:2012-08-14 13:37:49
【问题描述】:

我有一个使用网站 API 的应用程序。它返回这个 xml:

<xml>
  <id>48</id>
  <name>Nuno Horta</name>
  <user_lvl>2</user_lvl>
</xml>

我的代码是这个:

client.ExecuteAsync(request, response => {
                            var value = response.Content;
                            XElement loadedData = XElement.Parse(value);
                            var data = from query in loadedData.Descendants("xml")
                                       select new 
                                       {
                                           id = (int)query.Element("id"),
                                           name = (string)query.Element("name"),
                                       };
                            listBox.ItemsSource = data;
});

我想获取这两个值,id 和 name,以将它们保存在应用程序设置中,因此我试图在此处显示它们:

<TextBlock Text="XML Data:"/>
                <ListBox x:Name="listBox">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="10" >
                                <TextBlock Text="{Binding name}"/>
                                <TextBlock Text="{Binding id}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                   </ListBox>

顺便说一句,我正在使用 restsharp 发出请求 没有成功...任何想法来获得这些价值观?我需要使用其他 3 个 api 方法请求来执行此操作。谢谢!

【问题讨论】:

  • 然后会发生什么? “数据”的类型是什么?
  • 它没有发生任何事情..我期望同时获得两个值(id 和 name)来对它们做些什么。数据它只是数据绑定的一个变量,所以我可以在文本块上显示值。

标签: xml windows-phone-7 rest serialization


【解决方案1】:

您需要修改数据绑定以使用 Path,因为您要绑定到项目的属性。

所以你的 xaml 应该是

<TextBlock Text="XML Data:"/>  
            <ListBox x:Name="listBox">  
                <ListBox.ItemTemplate>  
                    <DataTemplate>  
                        <StackPanel Margin="10" >  
                            <TextBlock Text="{Binding Path=name}"/>  
                            <TextBlock Text="{Binding Path=id}"/>  
                        </StackPanel>  
                    </DataTemplate>  
                </ListBox.ItemTemplate>  
               </ListBox> 

更多关于路径绑定的信息是here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 2011-09-15
    相关资源
    最近更新 更多