在隔离存储中绑定图像

6
嘿, 我有一个用户可以搜索的项目列表。搜索结果显示在列表框中。每个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,所以任何帮助都将是很棒的。谢谢。


你是否忘记了绑定中的路径?如果你绑定到你的DataContext(而不是从它的路径),你应该通过{Binding .,Converter = {...}}或{Binding Path =.,Converter = {...}}来提及它。 - Benjamin Baumann
2个回答

6
代码存在一些问题。以下是可能在您的示例中缺失的一些内容:
首先,您绑定到转换器时没有指定要绑定的内容,因此它永远不会被调用。至少需要一个Path=(或简单地将属性名称作为快捷方式),否则不会调用转换器。您在哪里设置列表的ItemSource?
其次,传递的值是字符串文件名。您的转换器需要将它们用作文件名并基于该名称打开流。目前它正在尝试将名称用作字节数组。
最后,如果图像是固定集合,则将它们存储在ClientBin下的images文件夹中,并使用以下路径语法"/ images / imagename.jpg"等更加合理。这将自动涉及浏览器的缓存。您不需要转换器来完成这个任务。(关键是前导“/”。没有这个,Silverlight会认为图像位于当前模块中)
以下是一个完整的示例,使用ClientBin / images文件夹中显示的图像,运行时如下所示:
样本Xaml文件:
<UserControl x:Class="SilverlightApplication1.IsoImages"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ImageApp_Converter="clr-namespace:SilverlightApplication1" mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="White">
        <ListBox x:Name="ImageList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Background="AliceBlue">
                        <Image HorizontalAlignment="Left" Margin="8,8,0,4" Width="160" Height="120" Source="{Binding Path=Filename}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

下面是示例代码:
using System.Collections.Generic;
using System.Windows.Controls;

namespace SilverlightApplication1
{
    public partial class IsoImages : UserControl
    {
        public IsoImages()
        {
            InitializeComponent();
            List<ImageItem> images = new List<ImageItem>()
                                         {
                                             new ImageItem("/images/Image1.jpg"), 
                                             new ImageItem("/images/Image2.jpg"),
                                             new ImageItem("/images/Image3.jpg"),
                                             new ImageItem("/images/Image4.jpg")
                                         };
            this.ImageList.ItemsSource = images;
        }
    }

    public class ImageItem
    {
        public string Filename{ get; set; }
        public ImageItem( string filename )
        {
            Filename = filename;
        }
    }
}

0

你可能因为重复将同一文件加载到新的BitmapSource对象中而导致内存不足。你应该只创建大约10个BitmapSource对象,每个文件一个。然后通过将它们分配给Image.Source属性来重复使用这些BitmapSource实例。

一种方法是使用实现了IValueConverter接口的类,该类维护一个静态字典,将文件路径与BitmapSource键值对相关联。


谢谢您的帮助。我还无法使IValueConverter正常工作,但我已经更新了我的代码问题。 - XSL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接