简体   繁体   English

将 webService 数据绑定到 ListBox DataTemplate WP7

[英]Binding webService data to the ListBox DataTemplate WP7

I'm trying to read data using web service and display it on a costumized lisBox as below but it didn't work.我正在尝试使用 web 服务读取数据并将其显示在如下定制的 lisBox 上,但它不起作用。 "When i do the debugging my phone application screen does not show any list" “当我进行调试时,我的手机应用程序屏幕不显示任何列表”

XAML code: XAML 代码:

 <ListBox Height="500" HorizontalAlignment="Left" 
         Margin="8,47,0,0" 
         Name="friendsBox" VerticalAlignment="Top" Width="440">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Height="100" Width="100" 
                       VerticalAlignment="Top" Margin="0,10,8,0"
                       Source="{Binding Photo}"/>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Nom}" FontSize="28" TextWrapping="Wrap" Style="{StaticResource PhoneTextAccentStyle}"/>
                    <TextBlock   />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

C# code: C# 代码:

void friends_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        ListBoxItem areaItem = null;
        StringReader stream = new StringReader(e.Result);
        XmlReader reader = XmlReader.Create(stream);

        string Nom = String.Empty;
        string Photo = String.Empty;

        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element)
            {

                if (reader.Name == "nom")
                {

                    Nom = reader.ReadElementContentAsString();

                    areaItem = new ListBoxItem();
                    areaItem.Content = Nom;
                    friendsBox.Items.Add(Nom);
                }
                if (reader.Name == "photo")
                {

                    Photo = reader.ReadElementContentAsString();

                    areaItem = new ListBoxItem();
                    areaItem.Content = Photo;
                    friendsBox.Items.Add(Photo);
                }
            }
        }
    }
}

The problem is related to the inconsistent way you're managing your data.问题与您管理数据的方式不一致有关。 The data binding syntax in the XAML doesn't match the way you're manually loading items in the codebehind. XAML 中的数据绑定语法与您在代码隐藏中手动加载项目的方式不匹配。 Without seeing the structure of your XML, I'll make an inference that each of the items you're trying to show in the ListBox has two properties - nom and photo.在没有看到 XML 的结构的情况下,我将推断您尝试在 ListBox 中显示的每个项目都有两个属性 - nom 和 photo。 If that is the case, you can easily fix the problem you're experiencing by replacing the code in your codebehind with the following:如果是这种情况,您可以通过将代码隐藏中的代码替换为以下代码来轻松解决您遇到的问题:

// create this additional class to hold the binding data
public class ViewData
{
    public string Nom { get; set; }
    public string Photo { get; set; }
}

void friends_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
      var doc = XDocument.Load(new StringReader(e.Result));
      var items = from item in doc.Element("data").Elements("item")
                  select new ViewData
                  {
                      Nom = item.Element("nom").Value,
                      Photo = item.Element("photo").Value,
                  };
      friendsBox.ItemsSource = items;
  }
}

You will need to add a reference to System.Xml.Linq and add the appropriate using statement to your code.您需要添加对 System.Xml.Linq 的引用并将适当的 using 语句添加到您的代码中。

HTH!

Chris克里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM