これは銀色で作業し始めたばかりなので、これはちょっと疑問です。シルバーライトと2ウェイデータバインディングで画像をキャッシュする
私はCollectionViewSourceにバインドされたListBoxコントロールを持っています。このCollectionViewSourceはRIAサービスによって作成されます。データソース内の各アイテムには、サーバー上のイメージの文字列URLを含むThumbnailPathプロパティがあります。次に、アイテムテンプレートには、Imageコントロールと、Imageソースを設定するための依存関係プロパティSourceを持つユーザーコントロールがあります。ソースはアイテムのThumbnailPathプロパティにバインドされており、すべて正常に動作します。
しかし、Silverlightアプリケーションは、ListBoxのフィルタリングまたはページングが実行されるたびに、サーバーからイメージを要求します。私の考えは、ItemにBitmapImageフィールドを追加し、ImageコントロールのImageOpenedイベントでこのフィールドにイメージを格納し、次にThumbnailPathの代わりにこのイメージを使用することです。しかし、これを実装する方法は?双方向バインディング?私は2ウェイのデータバインディングについて読んでいたが、これをどうやって行うのかまだ分かっていなかった。誰かが良い事例や記事を教えてくれますか?
これは項目テンプレートである:
<ControlTemplate x:Key="ItemTemplate">
<Grid Width="104" Height="134" Margin="0,0,5,5" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
<Border BorderBrush="#FF000000" BorderThickness="2, 2, 2, 2" Margin="0,0,0,0" CornerRadius="0" />
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<my:ImageProgress Source="{Binding ThumbPath}"></my:ImageProgress>
<Grid Background="White" Margin="0,110,0,0" Opacity="0.5" Width="100" Height="20">
</Grid>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Center" Margin="0,110,0,0">
</TextBlock>
</Grid>
</Grid>
</ControlTemplate>
とユーザーコントロール:
<Grid x:Name="LayoutRoot" Background="Transparent" Width="100" Height="100">
<Image x:Name="Image" Stretch="Uniform" ImageFailed="ImageFailed" ImageOpened="ImageOpened">
</Image>
<TextBlock x:Name="FailureText" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="10" TextWrapping="Wrap" Margin="20,0,20,0" Visibility="Collapsed">
image not found
</TextBlock>
</Grid>
制御コード:
public partial class ImageProgress : UserControl
{
public ImageProgress()
{
InitializeComponent();
}
public BitmapImage Source
{
get { return (BitmapImage)GetValue(SourceProperty); }
set
{
SetValue(SourceProperty, value);
Image.Source = value;
}
}
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(BitmapImage), typeof(ImageProgress), new PropertyMetadata(new PropertyChangedCallback(OnSourceChanged)));
private static void OnSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var source = sender as ImageProgress;
if (source != null)
{
source.Source = (BitmapImage) e.NewValue;
}
}
void ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
var img = (Image) sender;
FailureText.Visibility = Visibility.Visible;
img.Visibility = Visibility.Collapsed;
}
private void ImageOpened(object sender, RoutedEventArgs e)
{
var img = (Image)sender;
// ???
}
}