在隔离存储中绑定图像

发布于 2024-10-01 02:58:30 字数 1135 浏览 4 评论 0原文

嘿。 我有一个用户可以搜索的项目列表。搜索结果显示在列表框中。每个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-10-08 02:58:30

显示的代码存在一些问题。您的示例中可能缺少一些内容:

首先,您对转换器的绑定没有指定要绑定到什么来获取其值,因此永远不会调用它。它至少需要一个 Path= (或者只是一个属性名称作为快捷方式),否则转换器将不会被调用。您在哪里设置列表的 ItemSource?

其次,传递的值是字符串文件名。您的转换器需要使用它们作为文件名并根据该名称打开一个流。目前它正在尝试将名称用作字节数组。

最后,如果图像是固定集,则将它们存储在 ClientBin 下的图像文件夹中并简单地使用以下路径语法“/images/imagename.jpg”等引用它们会更有意义。这将涉及浏览器的缓存自动地。您不需要转换器。 (关键是前导“/”。没有它,Silverlight 就会假定图像位于当前模块中)

alt text

下面是使用 ClientBin/images 文件夹中显示的图像的完整示例,运行时如下所示:

alt text

示例 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;
        }
    }
}

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)

alt text

Below is a complete example using the images shown in the ClientBin/images folder that looks like this when run:

alt text

Sample Xaml file:

<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>

The sample code behind is:

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;
        }
    }
}
赢得她心 2024-10-08 02:58:30

您可能会耗尽内存,因为您重复地将同一文件加载到新的 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 those BitmapSource instances by assigning them to Image.Source properties.

One way to do that is to use an implementation of IValueConverter that maintains a static dictionary of file path to BitmapSource key value pairs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文