【发布时间】:2012-01-23 17:25:43
【问题描述】:
我正在使用 RSS 提要在我的应用中显示内容。一切都很好,除了 gif 图像。我读过 Silverlight 不支持 gif 文件格式,所以我一直在尝试使用 ImageTools 插件。
似乎有很多示例,人们单击按钮并且图像显示在同一页面上,但我想在填充列表框时调用图像。
目前这是我所拥有的:
XAML:
xmlns:imagetools="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls"
....
<ListBox x:Name="Weather">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<imagetools:AnimatedImage Source="{Binding WeatherIcon, Converter={StaticResource DisplayGIF}}" />
<TextBlock Name="temperatureBlock" Text="{Binding WeatherTemperatureSummary}" TextWrapping="Wrap" Margin="12,0,0,0" FontSize="{Binding HeadlineSize}" Foreground="{Binding WeatherTemperatureSummary, Converter={StaticResource StylesAndColours}}" />
<TextBlock Name="summaryBlock" Text="{Binding Summary, Converter={StaticResource RssTextTrimmer}}" TextWrapping="Wrap" Margin="12,-6,0,10" FontSize="{Binding SummarySize}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
DisplayGif 类:
using ImageTools;
using ImageTools.IO;
using ImageTools.IO.Gif;
using System.Windows.Data;
using System.Text;
using System.IO;
using System.Windows.Media.Imaging;
public class DisplayGIF : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte[] buffer = Encoding.Unicode.GetBytes(value.ToString());
Stream stream = new MemoryStream(buffer);
ExtendedImage image = new ExtendedImage();
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
return image.ToBitmap(); // give error that image is not loaded
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
上述代码失败,因为图像尚未加载。是否有解决此问题的方法,甚至有更好/更简单的方法来显示 gif 图像?
编辑 1
根据 Ku6opr 的回答,我改变了我的班级。当我现在启动我的应用程序时,它挂起,因为我怀疑我没有正确管理线程。
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte[] buffer = Encoding.Unicode.GetBytes(value.ToString());
Stream stream = new MemoryStream(buffer);
ExtendedImage image = new ExtendedImage();
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
image.SetSource(stream);
EventWaitHandle Wait = new AutoResetEvent(false);
image.LoadingCompleted += (s, e) =>
{
Wait.Set();
};
Wait.WaitOne();
return image.ToBitmap();
}
编辑 2:
好的,原来我试图链接到的 gif 图像不是可热链接的! Ku6opr 提供的链接确实有效 - 只需确保您实际上可以链接到来自外部来源的图像;)
谢谢,
丰富
【问题讨论】:
标签: silverlight windows-phone-7 gif