2014-01-08 131 views
0

我正在構建我的第一個應用程序在Windows Phone 7中。我需要顯示來自Web服務的一些數據以及圖像。我能夠顯示數據但不知道如何顯示圖像。可以輸入需要更新的新數據。圖像將來自後臺,路徑將來自Web服務。我的網絡服務是:如何在windows phone 7應用程序中顯示來自後臺的圖像?

<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. 

</News_Description> 
    <Date_Start>2014-01-03</Date_Start> 
    <image_path>news.png</image_path> 
    </UserDetails> 

會有超過1個數據。我能夠顯示news_Title,news_description,Date_start。我的CS代碼是

public class Newss 
    { 
     public string News_Title { get; set; } 
     public string News_Description { get; set; } 
     public string Date_Start { get; set; } 
    } 




    public News() 
    { 
     InitializeComponent(); 

     KejriwalService.aapSoapClient client = new KejriwalService.aapSoapClient(); 
     client.getarvindNewsCompleted += new EventHandler<KejriwalService.getarvindNewsCompletedEventArgs>(client_getarvindNewsCompleted); 
     client.getarvindNewsAsync(); 
    } 

    void client_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.Date_Start = location.Element("Date_Start").Value; 
      listData.Add(data); 
     } 
     listBox1.ItemsSource = listData; 

    } 

我的XAML文件是

<ScrollViewer Margin="12,17,-12,144" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" AllowDrop="False" ManipulationMode="Control"> 
      <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> 
     </ScrollViewer> 

回答

0

添加到您的模型

public class Newss 
{ 
    public string News_Title { get; set; } 
    public string News_Description { get; set; } 
    public string Date_Start { get; set; } 
    public string Image_Path { get; set; } 
} 

在你的foreach循環設置圖像屬性

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.Date_Start = location.Element("Date_Start").Value; 
     Newss.Image_Path = location.Element("image_path").Value 
     listData.Add(data); 
    } 

在你的Xaml

<ScrollViewer Margin="12,17,-12,144" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" AllowDrop="False" ManipulationMode="Control"> 
     <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> 
         <Image Source="{Binding Path=Image_Path}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </ScrollViewer> 

很顯然,確保從XML負載的IMAGE_PATH數據是一個有效的URI,我想將其設置爲靜態圖像,如「http://static.bbci.co.uk/frameworks/barlesque/2.59.4/orb/4/img/bbc-blocks-dark.png

+0

的代碼不工作啓動。沒有錯誤也沒有迴應 – bhaku

+0

控制移動到app.xaml.cs中的以下行,當我在添加代碼後運行應用程序//代碼在未處理的異常上執行 private void Application_UnhandledException(object sender,ApplicationUnhandledExceptionEventArgs e) {如果(System.Diagnostics.Debugger.IsAttached) { //發生未處理的異常;打入調試器 System.Diagnostics.Debugger.Break(); } } – bhaku

+0

展開異常對象(e) - Message和Inner異常說了什麼? – FunksMaName

相關問題