【问题标题】:C# XDocument get all images from XML and show them in LongListSelectorC# XDocument 从 XML 中获取所有图像并在 LongListSelector 中显示它们
【发布时间】:2014-04-10 12:22:29
【问题描述】:

我有一个问题,我试图从 XML 中获取所有“拇指”图像链接,然后在 LongListSelector 中显示它们。

这是我的 LongListSelector 代码:

<phone:LongListSelector LayoutMode="Grid" GridCellSize="180,180" Margin="0,0,-12,0" ItemsSource="{Binding}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Grid Background="{StaticResource PhoneAccentBrush}" Margin="5">
                            <StackPanel>
                                    <Image Source="{Binding Images}"></Image>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
                </phone:LongListSelector>

和我的数据上下文:

this.DataContext = gd.GetGameItems;

读取xml和存储数据的代码:

var feedXml = XDocument.Parse(e.Result);

            var gameData = feedXml.Root.Descendants("Game").Select(x => new GetGame
            {
                ID = (int)x.Element("id"),
                GameTitle = (string)x.Element("GameTitle"),
                Platform = (string)x.Element("Platform"),
                ReleaseDate = (string)x.Element("ReleaseDate"),
                Images = new Uri(GetImages(x)),
            })
              .ToList();
            foreach (var item in gameData) GetGameItems.Add(item);

private ObservableCollection<GetGame> _GetGameItems = new ObservableCollection<GetGame>();
    public ObservableCollection<GetGame> GetGameItems
    {
        get
        {
            return this._GetGameItems;
        }
    }

public class GetGame
{
    public int ID { get; set; }
    public string GameTitle { get; set; }        
    public string Platform { get; set; }
    public string ReleaseDate { get; set; }
    public Uri Images { get; set; }
}

这是我试图从中获取所有图像的地方,现在我只能从一个图像链接中获取:

 private static string GetImages(XElement gameNode)
    {
        return "http://thegamesdb.net/banners/" + (string)gameNode.Descendants("boxart")
                               .FirstOrDefault().Attribute("thumb");
    }

这是 XML:

    <Data>
  <baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl>
  <Game>
    <id>2</id>
    <GameTitle>Crysis</GameTitle>
     <PlatformId>1</PlatformId>
     <Platform>PC</Platform>
     <ReleaseDate>11/13/2007</ReleaseDate>
     <Overview>
     From the makers of Far Cry, Crysis offers FPS fans the best-looking, most highly-        evolving gameplay, requiring the player to use adaptive tactics and total customization of    weapons and armor to survive in dynamic, hostile environments including Zero-G. Earth, 2019. A team of US scientists makes a frightening discovery on an island in the South China Sea. All contact with the team is lost when the North Korean Government quickly seals off the area. The United States responds by dispatching an elite team of Delta Force Operators to recon the situation. As tension rises between the two nations, a massive alien ship reveals itself in the middle of the island. The ship generates an immense force sphere that freezes a vast portion of the island and drastically alters the global weather system. Now the US and North Korea must join forces to battle the alien menace. With hope rapidly fading, you must fight epic battles through tropical jungle, frozen landscapes, and finally into the heart of the alien ship itself for the ultimate Zero G showdown.
    </Overview>
<ESRB>M - Mature</ESRB>
<Genres>
<genre>Shooter</genre>
</Genres>
<Players>4+</Players>
<Co-op>No</Co-op>
<Youtube>http://www.youtube.com/watch?v=i3vO01xQ-DM</Youtube>
<Publisher>Electronic Arts</Publisher>
<Developer>Crytek</Developer>
<Rating>8.1111</Rating>
<Images>
  <fanart>
    <original width="1920" height="1080">fanart/original/2-1.jpg</original>
    <thumb>fanart/thumb/2-1.jpg</thumb>
  </fanart>
  <fanart>
    <original width="1920" height="1080">fanart/original/2-2.jpg</original>
    <thumb>fanart/thumb/2-2.jpg</thumb>
  </fanart>
  <fanart>
    <original width="1920" height="1080">fanart/original/2-3.jpg</original>
    <thumb>fanart/thumb/2-3.jpg</thumb>
  </fanart>
  <fanart>
    <original width="1920" height="1080">fanart/original/2-4.jpg</original>
    <thumb>fanart/thumb/2-4.jpg</thumb>
  </fanart>
  <fanart>
    <original width="1920" height="1080">fanart/original/2-5.jpg</original>
    <thumb>fanart/thumb/2-5.jpg</thumb>
  </fanart>
  <fanart>
    <original width="1920" height="1080">fanart/original/2-6.jpg</original>
    <thumb>fanart/thumb/2-6.jpg</thumb>
  </fanart>
  <boxart side="back" width="1525" height="2162" thumb="boxart/thumb/original/back/2-1.jpg">boxart/original/back/2-1.jpg</boxart>
  <boxart side="front" width="1525" height="2160" thumb="boxart/thumb/original/front/2-1.jpg">boxart/original/front/2-1.jpg</boxart>
  <banner width="760" height="140">graphical/2-g2.jpg</banner>
  <banner width="760" height="140">graphical/2-g3.jpg</banner>
  <screenshot>
    <original width="1920" height="1080">screenshots/2-1.jpg</original>
    <thumb>screenshots/thumb/2-1.jpg</thumb>
  </screenshot>
  <clearlogo width="400" height="95">clearlogo/2.png</clearlogo>
</Images>
</Game>
</Data>

希望有人可以帮忙。

【问题讨论】:

  • 您没有说明当前代码会发生什么,也没有展示涉及的 XML 示例。
  • 抱歉,我现在添加了 XML。问题是我不知道如何收集所有图像链接,因为我无法让它与列表一起使用。我希望这能更好地解释它。

标签: c# xml linq binding datacontext


【解决方案1】:

你在找吗?

private static IEnumerable<Uri> GetImages(XElement gameNode)
    {
        return gameNode
                  .Descendants("boxart")
                  .Select(t => new Uri("http://thegamesdb.net/banners/" + (string)t.Attribute("thumb")));

还要改

 Images = new Uri(GetImages(x))

Images = GetImages(x).ToList()

【讨论】:

  • 是的,也许,我不知道如何收集所有图像缩略图链接并将它们显示在我的 LongListSelector 中。
  • 是的,但我需要将public Uri Images { get; set; } 设置为public List&lt;Uri&gt; Images { get; set; },对吗?但问题是,我对如何将它们添加到列表感到困惑,因为我不能在 linq 选择中执行 Images.Add(item)
  • 谢谢:)。但是你能告诉我如何在我的 longlistselector 中显示图像吗?
  • 你知道如何让我的 longlistselector 显示图像,我无法让它显示它们,我认为这是因为它们是一个列表中的一个可观察集合。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
相关资源
最近更新 更多