簡體   English   中英

從xml解析wp7應用程序中的圖像

[英]parsing image in a wp7 app from xml

我正在遵循本教程: http : //www.developer.nokia.com/Community/Wiki/Employees_app_with_XML_parsing_and_messaging_in_WP7

我有這個應用程序,用於顯示城市中的事件,每個事件都有一個圖像和標題,如果單擊一個事件,我將轉到詳細信息頁面,該頁面向我顯示較大的圖像,事件的標題和說明以及所有內容類似於教程,實際上工作得很好,是問題嗎? 它只顯示了一個事件。

在這里您可以看到我使用的供稿:

http://th05.deviantart.net/fs70/PRE/f/2012/226/0/7/xml_by_javabak-d5b1d16.png

這是我的活動課:

namespace Bluey
{
   [XmlRoot("rss")]
   public class Eventi
   {
       [XmlArray("channel")]
       [XmlArrayItem("item")]
        public ObservableCollection<Evento> Collect { get; set; }
   }
}

這是我的活動課

namespace Bluey
{
  public class Evento
  {
    [XmlElement("title")]
    public string title { get; set; }
    [XmlElement("enclosure")]
    public string image { get; set; }
    [XmlElement("description")]
    public string description { get; set; }
  }
}

我注意到在事件類中,如果將[XmlElement(“ enclosure”)]更改為[XmlElement(“ url”)],我將獲得所有事件,但沒有圖像

這是我的解析器

  public EventiPage()
    {
        InitializeComponent();
        // is there network connection available
        if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
        {
            MessageBox.Show("No network connection available!");
            return;
        }
        // start loading XML-data
        WebClient downloader = new WebClient();
        Uri uri = new Uri("http://www.applinesrl.com/appoggio/events/feed", UriKind.Absolute);
        downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(EventiDownloaded);
        downloader.DownloadStringAsync(uri);

    }

    void EventiDownloaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result == null || e.Error != null)
        {
            MessageBox.Show("There was an error downloading the XML-file!");
        }
        else
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Eventi));
            XDocument document = XDocument.Parse(e.Result);
            Eventi eventi = (Eventi)serializer.Deserialize(document.CreateReader());
            EventiList.ItemsSource = eventi.Collect; 
        }
    }

任何幫助將不勝感激,在建議中tnx!

迭戈

根據您發布的示例XML,您的圖像似乎已嵌入到機櫃節點中。 因此,Evento類是部分正確的。 您所要做的就是這樣:

public class Enclosure
{
    [XmlElement("url")]
    public string url { get; set; }
}

public class Evento
{
    [XmlElement("title")]
    public string title { get; set; }
    [XmlElement("enclosure")]
    public Enclosure image { get; set; }
    [XmlElement("description")]
    public string description { get; set; }
}

更新以顯示如何訪問圖像URL:

XmlSerializer serializer = new XmlSerializer(typeof(Eventi));
XDocument document = XDocument.Parse(e.Result);
Eventi eventi = (Eventi)serializer.Deserialize(document.CreateReader());
foreach (Evento obj in eventi.Collect)
{
    Debug.WriteLine("Title: {0}", obj.title);
    Debug.WriteLine("Img: {0}", obj.image.url); // Retrieving image URL
}

注意:XML中的某些值已經是使用base64編碼的圖像,而其他的則是URL。 因此,您應該對此進行驗證。 請在附件中找到我通過代碼和xml數據獲得的數據的圖像捕獲。 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM