windows - 如何在 Windows Phone 7 中绑定(bind)来自 xml 文件的动态多列表数据?

标签 windows linq windows-phone-7 c#-4.0 linq-to-xml

您好,我正在使用下面给出的 xml 文件,我尝试了很多代码从 xml 文件中获取多个列表,但我无法获取。

<?xml version="1.0" encoding="utf-8" ?>
  <root>
    <Categories>
      <Category name="Photos">
        <Articles>
          <article  title="Sherawat's">
            <Description>
               Hottie Sherawat 
            </Description>
            <FullContent>
             <style> img {padding:2px;} </style><p> <img alt=" Sherawat" pimcore_disable_thumbnail="true" pimcore_id="5853" pimcore_type="asset" src="http://feb2013/bolly---sherawat-s-upcoming-movie-dirty-politics/90_mallika-sherawat_bolly.jpg" style="width: 500px; height: 370px; float: left;" /></p> <p>Sherawat is all set to begin shooting for&nbsp;<em>Dirty Politics</em>&nbsp;where she plays Bhanwari Devi, a nurse whose murder hit the headlines last year. Meanwhile, she gets talking on&nbsp;Hollywood where she has not really had any meaty roles.</p>
            </FullContent>
            <thumb_image>
              <image  url="http://sss.com/Photo1.jpeg"/>"/>
            </thumb_image>
        </article>
        <article articleid="2684" title="Steals the Mai Show">
        <Description>
           <p> Actor Hrithik Roshan has always nursed a dream of working with acclaimed Director Kapur.</p>
        </Description>
       <FullContent>
        <div id="container" class="cf">
           <link rel="stylesheet" href="http://sss.com/imageslider/app/css/demo.css" type="text/css" media="screen" /> 
         <link rel="stylesheet" href="http://sss.com/imageslider/app/css/flexslider.css" type="text/css" media="screen" /> 
                 <div id="main" role="main"> <section class="slider"> <div class="flexslider"> <ul class="slides">
                   <li>Sonam Kapoor<img src="http://sss.com//website/var/tmp/thumb_5814_1_01feb2013__appfeed.jpeg" alt="Kapoor"/></li>
          </ul>
          </li>
         </div>
       </FullContent>
       <thumb_image>
            <image  url="http://Photo2.jpeg"/>"/>
       </thumb_image>
     </article>
 </Articles>
 </Category>
 <Category name="Videos">
<Articles>
   <article articleid="415" title=" Dirty Politics">
      <Description>
         Sherawat speaks about the men whom she’s over the moon about
      </Description>
      <FullContent>
        <style> img {padding:2px;} </style><p> <img alt="Sherawat" pimcore_disable_thumbnail="true" pimcore_id="5853" pimcore_type="asset" src="http://sss.com/bolly/feb2013/bolly---sherawat-s-upcoming-movie-dirty-politics/90_sherawat_bolly.jpg" style="width: 500px; height: 370px; float: left;" /></p>
      </FullContent>
      <thumb_image>
        <image  url="http://Video1.jpeg"/>"/>
      </thumb_image>
    </articles>
    <article articleid="68" title="Digital!">
     <Description>
         <p> Touch, tap, flip, slide! You don&#39;t, you experience it.</p>
      </Description>
      <FullContent>
        <p> Touch, tap, flip, slide! You don&#39;you experience it.</p> <br/><br/><br/> <br/><br/>
      </FullContent>
      <thumb_image>
        <image  url="http://Video2.jpeg"/>"/>
      </thumb_image>
    </article>
 </Article>
</Category>
 <Category name="Bolly">
   <Articles>
     <article articleid="415" title="upcoming movie">
       <Description>
           the men whom she’s over the moon about
       </Description>
       <FullContent>
        <style> img {padding:2px;} </style><p> <img alt="Sherawat" pimcore_disable_thumbnail="true" pimcore_id="5853" pimcore_type="asset" src="http://sss.com/bolly/feb2013/bolly---sherawat-s-upcoming-movie-dirty-politics/90_sherawat_bolly.jpg" style="width: 500px; height: 370px; float: left;" /></p>
      </FullContent>
       <thumb_image>
         <image url="http://sss.com/website/var/tmp/thumb_5854_90_mallika-sherawat_thumb_bolly__forfeed.jpeg"/>
       </thumb_image>
       </articles>
       <article articleid="436" title="Surprise Package">
         <Description>
           There was more than just good music at the trio's recent performance
         </Description>
         <FullContent>
           <style> img {padding:2px;} </style><p> <img alt="Akcent" pimcore_disable_thumbnail="true" pimcore_id="6110" pimcore_type="asset" src="http://dev2.mercuryminds.com/global/feb2013/surprise-package-at-akcent-concert/18_akcent_global.jpg" style="width: 370px; height: 500px; float: left;" /></p>
         </FullContent>
         <thumb_image>
           <image url="http://sss.com/website/var/tmp/thumb_6109_18_akcent_thumb__forfeed.jpeg"/>
         </thumb_image>
       </article>
    </Article>
 </Category>
 </Categories>
</root>

我的 MainPageXaml.cs 代码

 public MainPage()
    {
        InitializeComponent();

        WebClient downloader = new WebClient();
        Uri uri = new Uri("http://sss.com/webservices/new_feed_articls.xml", UriKind.Absolute);
        downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(NotchsDownloaded);
        downloader.DownloadStringAsync(uri);
    }

  void NotchsDownloaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Result == null || e.Error != null)
        {
            MessageBox.Show("There was an error downloading the XML-file!");
        }
         else
         {
             ParseXMLFile(e.Result);
         }
    }

    void ParseXMLFile(string  dataInXmlFile)
    {
        try
        {
          //Parsing XML File


            XDocument xmlDoc = XDocument.Parse(dataInXmlFile);

            var query = from l in xmlDoc.Descendants("Category")
                        select new Data
                        {
                            name = (string)l.Attribute("name").Value,
                            Titles = l.Element("Articles").Elements("article")
                                     .Select(s => s.Attribute("title").Value)
                                     .ToList(),


                            Image = l.Element("Articles").Elements("article")
                                     .Elements("thumb_image").Elements("image")
                                     .Select(x => x.Attribute("url").Value).ToList()
                        };
                    foreach (var result in query)
                    {
                      Console.WriteLine(result.name);
                         foreach (var detail in result.Titles)
                         {
                           Console.WriteLine(detail);
                         }
                     }
                     List.ItemsSource = query.ToList();

        }
        catch(Exception e)
        {
        }

    }

我的类(class)文件

public class Data
{

    [XmlAttribute("name")]
        public string name { get; set; }

    [XmlAttribute("title")]
    public List<string> Titles { get; set; }

    [XmlAttribute("url")]
    public List<string> Image { get; set; }

    [XmlElement("Description")]
    public string Description { get; set; }

    [XmlElement("FullContent")]
    public string FullContent { get; set; }


}

[XmlRoot("root")]
public class DataArray
{
    [XmlArray("Categories")]
    [XmlArrayItem("Category")]
    [XmlArrayItem("Articles")]
     [XmlArrayItem("article")]
    public ObservableCollection<Data> Collection { get; set; }
}

我的 XAML 文件

<ListBox x:Name="List" ItemsSource="{Binding}"
           Margin="0,-0.25,2,-6" Grid.Row="3" HorizontalAlignment="Right" Width="478" d:LayoutOverrides="VerticalMargin" Grid.RowSpan="2">
        <ListBox.ItemTemplate>
            <DataTemplate>
                  <StackPanel Margin="0,0,0,0"  Orientation="Vertical" Grid.ColumnSpan="2" Grid.RowSpan="3" x:Name="ControlsPanel"
                        Grid.Column="0"
                        Height="160"
                        VerticalAlignment="Top">
                        <StackPanel Background="#eb2427" Orientation="Vertical">
                            <TextBlock Grid.Row="1"  FontFamily="Calibri" FontSize="34" FontWeight="Bold"  FontStyle="Normal" Margin="10,0,0,0"
                                Text="{Binding name}" 
                                   />               
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Grid.Row="1" FontFamily="Calibri" FontSize="32" Foreground="#a7a9ac" Width="Auto"
                                Text="{Binding Titles}" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Visible"/>
                        </StackPanel>
                        <StackPanel>
                                <Image Source="{Binding Image}" Width="Auto"></Image>
                        </StackPanel>
                    </StackPanel>       
            </DataTemplate> 
        </ListBox.ItemTemplate>
    </ListBox>

我尝试了很多代码,但我得到了相同的结果。输出没有变化。我在我的输出屏幕短裤下面给出了

enter image description here

我想要这样的输出

enter image description here

嗨,我尝试了很多方法,但很长时间都无法解决这个问题。所以现在我告诉你我的期望。请任何人考虑我的问题并给我任何代码来解决这个问题。

最佳答案

首先,将 Image 属性重命名为 Images,因为它是一个图像列表,而不是单个图像。

然后,在 XAML 代码中,将该图像列表的显示方式从显示一个图像调整为显示多个。这与显示集合的方式相同 - 通过使用 ListBox(或其他适当的 ItemsControl)。将 XAML 更改为:

<ListBox ItemsSource="{Binding Image}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}" Width="Auto"></Image>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这将显示一个图像列表,而不是显示一个包含一个图像的 StackPanel。但它会垂直显示它们而不是水平显示。查看以下主题以获取更多信息:How do I get a Horizontal ListBox to scroll horizontally in WP7? .

关于windows - 如何在 Windows Phone 7 中绑定(bind)来自 xml 文件的动态多列表数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15518176/

相关文章:

linq - 使用LINQ计算与上一项的差异

silverlight - 如何使用 WP7 C# 从 twitter 发帖?

c# - 捆绑的 GTK# 应用程序无法在新安装的 Win7 上运行

C#剪贴板直接复制粘贴

c# - 如何创建正则表达式以匹配不包含 where 子句的 linq 语句?

c# - 找不到 System.Xml.Xpath

windows-phone-7 - 如何检测WP7中的主题何时更改?

asp.net - Windows azure session 管理配置

windows - 如何在 Windows 中自动启动 XAMPP 和 Laravel 项目

c# - linq selectmany 扁平化多个级别