C# 如何在WindowsPhone8中绑定listbox中的xml图像

C# 如何在WindowsPhone8中绑定listbox中的xml图像,c#,image,windows-phone-8,C#,Image,Windows Phone 8,如何在WindowsPhone8中绑定xml图像? 我已经做了所有的方法,但它不工作,每当我调试应用程序,它包含一个图像源,但图像不显示 代码: List lst=new List(); lst=(从文档子体(“行”)中的查询选择新列表 {Id=Convert.ToInt64(query.Element(“Id”).Value), Icon=query.Element(“Icon”).Value, xyz=Convert.ToInt64(query.Element(“xyz”).Value),

如何在WindowsPhone8中绑定xml图像? 我已经做了所有的方法,但它不工作,每当我调试应用程序,它包含一个图像源,但图像不显示

代码:

List lst=new List();
lst=(从文档子体(“行”)中的查询选择新列表
{Id=Convert.ToInt64(query.Element(“Id”).Value),
Icon=query.Element(“Icon”).Value,
xyz=Convert.ToInt64(query.Element(“xyz”).Value),
Url=query.Element(“Url”).Value,Name=query.Element(“Name”).Value}.ToList();
listBox1.DataContext=lst;
XAML代码:


我认为您可以尝试使用转换器(如果您在internet上搜索绑定和转换器,您将找到许多链接、教程等)。一个简单的代码可以是这样的(我没有尝试过):

在XAML中:

...
xmlns:common="clr-namespace:YourNamespace"
...
<phone:PhoneApplicationPage.Resources>
    <common:ToImageSource x:Key="converter"/>
</phone:PhoneApplicationPage.Resources>
...
<Image Source="{Binding Icon, Converter={StaticResource converter} }" Stretch="Uniform" HorizontalAlignment="Center" Height="50" Width="50" VerticalAlignment="Top"/>
您还可以阅读有关绑定和转换器的更多信息。

这是我的.cs文件:

public static BitmapImage getImage(string img)
{
    byte[] filebytes = Convert.FromBase64String(img);
    MemoryStream ms = new MemoryStream(filebytes, 0, filebytes.Length);
    BitmapImage image = new BitmapImage();
    image.SetSource(ms);
    return image;
}

Icon = getImage(query.Element("Icon").Value);
和XAML文件:

<Image Source="{Binding Icon}" Stretch="Uniform" HorizontalAlignment="Center" Height="50" Width="50" VerticalAlignment="Top"/>


“我已经完成了所有的方法,但不起作用”请在有问题的帖子中说明你所做的..List lst=new List();lst=(从文档子体(“行”)中的查询选择新列表{Id=Convert.ToInt64(query.Element(“Id”).Value),Icon=query.Element(“Icon”).Value,xyz=Convert.ToInt64(query.Element(“xyz”).Value),Url=query.Element(“Url”).Value,Name=query.Element(“Name”).Value}).ToList();listBox1.DataContext=lst;您如何绑定图像?您是否检查它是否是正确的URL?xml图像是什么意思?您可以从xml保存图像…它只是文本,所以您有图像路径是您的xml吗?@user3115090您还可以显示XAML代码吗?thnks寻求帮助兄弟,但我应该在哪里编写代码phoneapplicationpage.resources等。我无法获取it@user3115090xmlns:-将Page.xaml添加到其他xmlns之间的顶部。您添加的phoneapplicationpage.resources,例如在此块之后(网格之前)。您还应该阅读一些,可能还有一些。我知道那个兄弟,但在我的XAML设计器中,我无法获得应用程序资源标签之间的“公共”。我正在使用visual studio 2013,windows phone8@user3115090您是否有可能共享您的xaml和cs文件?我得到了@Romasz的解决方案。我的web服务对xml图像的响应是Base64,所以我将Base64转换为位图图像,并得到了图标。谢谢这当然是一种工作方法。我得到了一个解决方案,我已经将base64字符串转换为位图imageIMO。使用具有指定getter的转换器或属性会更好。然后,可以使用INotifyPropertyChanged轻松实现它。
public class ToImageSource : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {       
     if (value == null) return null;
     else
     {
        byte[] imageBytes = Convert.FromBase64String(value);

        using (MemoryStream stream = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
          stream.Write(imageBytes, 0, imageBytes.Length);
          BitmapImage bitmap = new BitmapImage();
          bitmap.SetSource(stream);
          return bitmap;
        }         
     }
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {   // implement ConvertBack      }
}
public static BitmapImage getImage(string img)
{
    byte[] filebytes = Convert.FromBase64String(img);
    MemoryStream ms = new MemoryStream(filebytes, 0, filebytes.Length);
    BitmapImage image = new BitmapImage();
    image.SetSource(ms);
    return image;
}

Icon = getImage(query.Element("Icon").Value);
<Image Source="{Binding Icon}" Stretch="Uniform" HorizontalAlignment="Center" Height="50" Width="50" VerticalAlignment="Top"/>