如何在列表框中显示来自webservice的数据

时间:2014-01-07 11:48:29

标签: c# windows-phone-7

我想在wp的listbox中显示来自webservice的数据。 websevice中的数据包含3个字段和图像,并且必须重新编译数据,即当输入新数据时,它应该首先出现。在我构建我的第一个应用程序时,我发现它有点困难。请帮忙。我的.cs代码到现在为止

namespace KejriwalPhoneApp
{
    public partial class News : PhoneApplicationPage
{
    public class Newss
    {
        public string News_Title { get; set; }
        public string News_Description { get; set; }
        public string Date_Start { get; set; }
    }

    public class NewsList: List<Newss>
    {
        public NewsList()
        {


        }
    }

    public News()
         {
             InitializeComponent();

            KejriwalService.aapSoapClient myClient = new KejriwalService.aapSoapClient();
            myClient.getarvindNewsCompleted += new EventHandler<KejriwalService.getarvindNewsCompletedEventArgs>(myClient_getarvindNewsCompleted);
            myClient.getarvindNewsAsync();

         }

       void myClient_getarvindNewsCompleted(object sender, KejriwalService.getarvindNewsCompletedEventArgs e)
          {
              listBox1.ItemsSource = e.Result;

          }

   }
}

我的数据集是:

<string><NewDataSet>
  <UserDetails>
    <id>5</id>
    <News_Title>Audit of Electricity Companies</News_Title>
    <News_Description> Rejecting the contention of private power distributors, the Delhi government today ordered an audit of their finances by the government's national auditor or Comptroller and Auditor General (CAG), fulfilling yet another election promise of the Aam Aadmi Party.

&amp;quot;We have ordered an audit of the private power distribution companies. The CAG has said it will do the audit,&amp;quot; Chief Minister Arvind Kejriwal said. He also said Lieutenant Governor Najeeb Jung's order on the audit of the companies will go to CAG Shashi Kant Sharma tomorrow.</News_Description>
<Date_Start>2014-01-03</Date_Start>
<image_path>news.png</image_path>

从这里我需要在一个列表中显示news_title,news_Description,Date_Start,image,这应该是可点击的,并且将有超过1个数据

我的xaml文件是

<ListBox Name="listBox1" Margin="38,86,38,562">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=News_Title}"></TextBlock>
                        <TextBlock Text="{Binding Path=News_Description}"></TextBlock>
                        <TextBlock Text="{Binding Path=Date_Start}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox> 

1 个答案:

答案 0 :(得分:0)

如果我猜对了你的myClient_getarvindNewsCompleted处理程序以xml字符串格式返回数据。因此,您应首先解析xml数据,然后将数据绑定到ListBox.ItemSource。以下是可能对您有所帮助的解决方案。

您需要在项目中添加'System.Xml.Linq'和'System.Linq'dll的引用

 void myClient_getarvindNewsCompleted(object sender, KejriwalService.getarvindNewsCompletedEventArgs e)
          {
string result = e.Result.ToString();
 List<Newss> listData = new List<Newss>();
            XDocument doc = XDocument.Parse(result);
            // Just as an example of using the namespace...
            //var b = doc.Element("NewDataSet").Value;
            foreach (var location in doc.Descendants("UserDetails"))
            {
                Newss data = new Newss();
                data.News_Title = location.Element("News_Title").Value;
                data.News_Description = location.Element("News_Description").Value;
                data.News_Date_Start = location.Element("Date_Start").Value;
                listData.Add(data);
            }
              listBox1.ItemsSource = listData;

          }