XSL*_*XSL 6 c# silverlight binding isolatedstorage windows-phone-7
嘿.我有一个用户可以搜索的项目列表.搜索结果显示在列表框中.每个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;
}
}
Run Code Online (Sandbox Code Playgroud)
我把它放在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}}" />
Run Code Online (Sandbox Code Playgroud)
我在我的BitmapConverter中设置了一个断点,但它从未被调用过.我之前从未使用过IValueConverter,所以任何帮助都会很棒.谢谢
显示的代码中存在一些问题.您的示例可能只是遗漏了一些内容:
首先,你的绑定到转换器没有指定什么绑定到获得它的价值,所以它永远不会被调用.它至少需要一个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>
Run Code Online (Sandbox Code Playgroud)
后面的示例代码是:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)