【问题标题】:Binding image in Isolated Storage隔离存储中的绑定图像
【发布时间】:2011-05-06 01:49:48
【问题描述】:

嘿。 我有一个用户可以搜索的项目列表。搜索结果显示在列表框中。每个animal 对象都有一个指向独立存储中图像的路径。将 listboxitem 中的 Image 控件绑定到隔离存储中的图像的最快方法是什么?我见过的示例倾向于显示来自互联网而不是独立存储的图像。如果我有大约 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={...}}提及它>

标签: c# silverlight binding windows-phone-7 isolatedstorage


【解决方案1】:

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

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

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

最后,如果图像是一个固定的集合,将它们存储在 ClientBin 下的图像文件夹中并使用以下路径语法“/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;
        }
    }
}

【讨论】:

    【解决方案2】:

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

    一种方法是使用IValueConverter 的实现,该实现维护BitmapSource 键值对的文件路径静态字典。

    【讨论】:

    • 感谢您的帮助。我不能完全让 IValueConverter 工作,但我已经用我的代码更新了这个问题。
    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多