2014-01-07 51 views
0

我想顯示web服務在wp列表框中的數據。 web服務中的數據包含3個字段以及一個圖像,並且數據必須被刷新,即當輸入新數據時,它應該首先出現。當我構建我的第一個應用程序時,我發現它有點困難。請幫忙。我的.cs代碼,直至現在如何顯示web服務在列表框中的數據

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,在一個列表中的圖像應該是點擊而且會有超過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> 
+0

什麼是你的代碼的問題?什麼類型是'e.Result'? – har07

+0

使用lambda表達式遍歷結果並將它們映射到Newss數據模型的集合。然後,將您的列表框ItemsSource設置爲newss集合 – kindasimple

回答

0

如果我猜對了你的myClient_getarvindNewsCompleted處理程序以xml字符串格式返回數據。所以你應該首先解析你的xml數據,然後你應該將數據綁定到ListBox.ItemSource。以下是可能對您有所幫助的解決方案。

您需要添加「System.Xml.Linq的」的refrence和「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; 

      } 
相關問題