【问题标题】:How to bind dynamic multi listing data from xml file in windows phone 7?如何在 windows phone 7 中绑定来自 xml 文件的动态多列表数据?
【发布时间】:2013-03-20 08:01:30
【问题描述】:

您好,我正在使用下面给出的 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)
        {
        }

    }

我的班级档案

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>

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

我想要像

这样的输出

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

【问题讨论】:

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


    【解决方案1】:

    首先,将 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?

    【讨论】:

    • 非常感谢......,我希望你的回答。它对我非常有用。再次感谢你亲爱的......
    • 嗨,我有水平列表,但我不能,如果我点击任何项目想要导航另一个页面。
    • 好吧,您需要为此设置 Tap 处理程序,然后检测哪个图像被点击了。这个问题不在您原来的问题中。
    【解决方案2】:

    您必须在您的ListBox.ItemTemplate 中创建另一个ListBox,而不是那个部分:

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

    【讨论】:

    • 您好,感谢 replay。我已经尝试过嵌套列表框。您的意思是 listbox.itemtemplate 而不是 listbox?请写下示例代码
    猜你喜欢
    • 2012-05-30
    • 2012-09-11
    • 2023-03-19
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    相关资源
    最近更新 更多