在隔离存储中绑定图像
嘿。 我有一个用户可以搜索的项目列表。搜索结果显示在列表框中。每个animal
对象都有一个到独立存储中的图像的路径。将列表框项内的图像控件绑定到独立存储中的图像的最快方法是什么?我见过的示例倾向于显示来自互联网的图像,而不是独立存储。如果我有大约 10 张图像,它似乎会占用所有内存并崩溃。谢谢
编辑:
我在我的 BitmapConverter
类(继承 IValueConverter)中使用它,
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value !=null)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream((Byte[]) value));
return bitmapImage;
}
else
{
return null;
}
}
我在 AppResource.xaml 文件的顶部有这个:
<ImageApp_Converter:BitmapConverter x:Key="bmpConverter" />
In my style, within the AppResource.xaml file:
<Image HorizontalAlignment="Left" Margin="8,8,0,4" Width="160" Height="120" Source="{Binding Converter={StaticResource bmpConverter}}" />
我在 BitmapConverter 中设置了一个断点,但从未被调用。我以前从未使用过 IValueConverter,所以任何帮助都会很棒。谢谢
Hey.
I have a list of items that the user can search. The search results are displayed in a listbox. Each animal
object has a path to an image in Isolated Storage. What's the quickest way to bind my Image control inside the listboxitem to the image in the isolated storage? Examples I've seen tend to display images from the internet rather than Isolated Storage. If I have around 10 images, it seems to take up all the memory and crash. thanks
EDIT:
I'm using this in my BitmapConverter
class (inherits IValueConverter)
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value !=null)
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream((Byte[]) value));
return bitmapImage;
}
else
{
return null;
}
}
I have this at the top of my AppResource.xaml file:
<ImageApp_Converter:BitmapConverter x:Key="bmpConverter" />
In my style, within the AppResource.xaml file:
<Image HorizontalAlignment="Left" Margin="8,8,0,4" Width="160" Height="120" Source="{Binding Converter={StaticResource bmpConverter}}" />
I set a breakpoint in my BitmapConverter, but it's never called. I've never used IValueConverter before, so any help would be great. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显示的代码存在一些问题。您的示例中可能缺少一些内容:
首先,您对转换器的绑定没有指定要绑定到什么来获取其值,因此永远不会调用它。它至少需要一个 Path= (或者只是一个属性名称作为快捷方式),否则转换器将不会被调用。您在哪里设置列表的 ItemSource?
其次,传递的值是字符串文件名。您的转换器需要使用它们作为文件名并根据该名称打开一个流。目前它正在尝试将名称用作字节数组。
最后,如果图像是固定集,则将它们存储在 ClientBin 下的图像文件夹中并简单地使用以下路径语法“/images/imagename.jpg”等引用它们会更有意义。这将涉及浏览器的缓存自动地。您不需要转换器。 (关键是前导“/”。没有它,Silverlight 就会假定图像位于当前模块中)
下面是使用 ClientBin/images 文件夹中显示的图像的完整示例,运行时如下所示:
示例 Xaml 文件:
后面的示例代码是:
There are a few problems in the code shown. Some may just be missing from your example:
Firstly, your binding to the converter does not specify what to bind to to get its value, so it is never called. At a minimum it needs a Path= (or simply a property name as short-cut) or the converter will not be called. Where are you setting the ItemSource of your list?
Secondly, the values getting passed are string file names. Your converter needs to use them as filenames and open a stream based on that name. At the moment it is trying to use the names as byte arrays.
Finally, if the images are a fixed set, it would make more sense to store them in an images folder under ClientBin and simply refer to them with the following path syntax "/images/imagename.jpg" etc. This will involve the browser's caching automatically. You do not need a converter for that. (The key is the leading "/". Without that Silverlight assumes the images are in the current module instead)
Below is a complete example using the images shown in the ClientBin/images folder that looks like this when run:
Sample Xaml file:
The sample code behind is:
您可能会耗尽内存,因为您重复地将同一文件加载到新的
BitmapSource
对象中。您应该只为每个文件创建“大约 10 个”BitmapSource
对象。然后通过将这些BitmapSource
实例分配给Image.Source
属性来重新使用它们。一种方法是使用
IValueConverter
的实现,它维护BitmapSource
键值对的文件路径静态字典。You are probably running out of memory because you are repeatedly loading the same file into new
BitmapSource
objects. You should create only "around 10"BitmapSource
objects one for each file. Then re-use thoseBitmapSource
instances by assigning them toImage.Source
properties.One way to do that is to use an implementation of
IValueConverter
that maintains a static dictionary of file path toBitmapSource
key value pairs.