【问题标题】:List of Images from absolute path in Listbox列表框中绝对路径的图像列表
【发布时间】:2014-12-23 07:07:40
【问题描述】:

我正在开发 windows phone 8.1 应用程序,

我有一个绝对路径的 URL,我必须显示每次动态变化的图像。

如何在 ListBox 中显示它们?

这是我的 XML 文件:

<root> 
  <row> 
    <Id>1234</Id>
    <Name>ABCD</projectName> 
    <isImage>1</isImage> 
  </row> 

 <row> 
   <Id>5678</Id> 
   <Name>PQRS</Name> 
   <isImage>1</isImage>
  </row>
</root>

在下面查看我的 XAML 代码:

<ListBox x:Name="listBox1" Width="480" Height="677" HorizontalAlignment="Left" Margin="0,0,0,59" VerticalAlignment="Top" ItemsSource="{Binding}" SelectedItem="{Binding}" SelectionMode="Extended">

<ListBox.ItemTemplate>
  <DataTemplate>

    <StackPanel Orientation="Horizontal" Background="White" Height="80" Margin="0,10,0,0">

     <Image x:Name="image1" Source="{Binding isImage}" Stretch="Uniform" HorizontalAlignment="Center" Height="70" Width="90" Margin="0,0,0,0"/>

      </StackPanel>

   </DataTemplate>
 </ListBox.ItemTemplate>

这是我的代码:

XDocument doc = XDocument.Parse(e.Result);

var nodes = doc.Descendants("row").ToList();

for (int i = 0; i < nodes.Count; i++)
{
   string newid = nodes[i].Element("Id").Value;

   string uri = "https://www.XYZ.com/abc/getDocument.htm?username=" + name + "&password=" + pwd + "&Id=" + newid;

   List<LIST> list = new List<LIST>();

   list = (from query in doc.Descendants("row")
       select new LIST
       {
         Id = query.Element("Id").Value,
         Name = query.Element("Name").Value,
         isImage = uri
       }).ToList();

   listBox1.DataContext = list;

}

我只得到“Counting Nodes”的最后一个“Id”,这就是问题所在。

我该如何解决,请帮帮我。

任何帮助将不胜感激。

谢谢!!

【问题讨论】:

    标签: c# listbox windows-phone-8.1 absolute-path


    【解决方案1】:

    问题是你每次都在 For 循环中绑定,一旦所有元素都添加到列表中,尝试绑定它。

    for (int i = 0; i < nodes.Count; i++)
    {
      Your logic
    }
    listBox1.DataContext = list;
    

    【讨论】:

    • 我得到“六个”后代节点,所以“行”有“六个”节点。但我只得到一张图片六次。我试过你的代码。 @Sajeetharan
    • 因为你绑定的是同一张图片!
    • 那怎么处理呢,这两天卡在这里了。请大哥帮帮我。谢谢。 @Sajeetharan
    【解决方案2】:

    我找到了解决办法。

    请看下面的代码:

    XDocument doc = XDocument.Parse(e.Result);
    List<LIST> list = new List<LIST>();
    var nodes = doc.Descendants("row").ToList();
    
     for (int i = 0; i < nodes.Count; i++)
     {
        string newid = nodes[i].Element("Id").Value;
        string uri = "https://www.XYZ.com/abc/getDocument.htm?username=" + name + "&password=" + pwd + "&Id=" + newid;
    
        list.Add(new LIST() { isImage = uri});
     }
    
     listBox1.DataContext = list;
    

    这很好用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 2018-06-11
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      相关资源
      最近更新 更多