6

おい。ユーザーが検索できるアイテムのリストがあります。検索結果はリストボックスに表示されます。各animalオブジェクトには、Isolated Storage 内のイメージへのパスがあります。listboxitem 内のイメージ コントロールを分離ストレージ内のイメージにバインドする最も簡単な方法は何ですか? 私が見た例では、Isolated Storage ではなく、インターネットからの画像を表示する傾向があります。約 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 を使用したことがないので、どんな助けも素晴らしいでしょう. ありがとう

4

2 に答える 2

6

示されているコードにはいくつかの問題があります。いくつかはあなたの例から単に欠落しているかもしれません:

まず、コンバーターへのバインドでは、値を取得するためにバインドする対象が指定されていないため、呼び出されることはありません。少なくとも、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;
        }
    }
}
于 2010-11-08T11:27:51.467 に答える
0

BitmapSource同じファイルを新しいオブジェクトに繰り返しロードしているため、メモリが不足している可能性があります。BitmapSourceファイルごとに「約 10」のオブジェクトのみを作成する必要があります。BitmapSource次に、これらのインスタンスをプロパティに割り当てて再利用しImage.Sourceます。

IValueConverterこれを行う 1 つの方法は、ファイル パスとBitmapSourceキー値のペアの静的辞書を保持する の実装を使用することです。

于 2010-11-06T17:28:43.503 に答える