在独立存储图像的结合 [英] Binding image in Isolated Storage

查看:104
本文介绍了在独立存储图像的结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哎。
  我有用户可搜索项的列表。搜索结果显示在列表框。每个动物对象必须在独立存储图像的路径。什么是绑定在独立存储的ListBoxItem的对图像内的我的形象控制的最快方法?我见过的例子倾向于从互联网上,而不是隔离储存显示图像。如果我身边有10幅图像,似乎采取了所有的内存和崩溃。谢谢

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

编辑:

我在我的 BitmapConverter 类使用这个(继承的IValueConverter)

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

我有这个在我鸭presource.xaml文件的顶部:

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}}"   />

我设置我的BitmapConverter断点,但它永远不会被调用。我以前从未使用过的IValueConverter,所以任何帮助将是巨大的。谢谢

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

推荐答案

有显示在code一些问题。有些人可能只是从你的例子里缺了:

There are a few problems in the code shown. Some may just be missing from your example:

首先,您绑定到该转换器没有指定的什么的结合来获得它的价值,所以它永远不会被调用。它需要一个路径=最小值(或仅仅是一个属性名作为短切)或转换器将不会被调用。你在哪里设置你的列表的ItemSource?

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.

最后,如果图像是一组固定的,它将使更多的意义将它们存储在的ClientBin下的图像文件夹,并简单地把它们具有以下路径语法/images/imagename.jpg等,这将涉及浏览器的自动缓存。你并不需要一个转换器。 (关键是领先的/。如果没有Silverlight的假设图像是在当前模块中代替)

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)

下面是一个使用的的ClientBin / images文件夹中运行时,看起来像这样显示图像的完整例子:

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

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

样品code后面的是:

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

这篇关于在独立存储图像的结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆