我有一个 WPF 应用程序,其中从 ThreadPool.QueueUserWorkItem() 下载图像。在 UI 中,我有一个 DispatcherTimer,它检查应该缓存图像的文件夹,如果找到,假设将它们显示为两个 Border 元素和一个 Button 元素的背景。
我可以看到文件被下载到 filePath,并且可以逐步执行创建 BitmapImage 对象的代码,但我没有看到屏幕上呈现的任何内容。
相关代码贴在下面..
<Border x:Name="leftImage"
BorderBrush="Transparent"
BorderThickness="0"
HorizontalAlignment="Left"
Height="220"
VerticalAlignment="Top"
Width="99">
<Button Template="{StaticResource LeftArrow}"
Width="20"
Height="20"
HorizontalAlignment="Left"
Margin="10,0,0,0" />
</Border>
<Border x:Name="rightImage"
BorderBrush="Transparent"
BorderThickness="0"
HorizontalAlignment="Right"
Height="220"
VerticalAlignment="Top"
Width="99"
Grid.Column="2">
<Button Template="{StaticResource RightArrow}"
Width="20"
Height="20"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Margin="0,0,10,0" />
</Border>
<Button x:Name="center"
Grid.Column="1"
HorizontalAlignment="Center"
Style="{StaticResource BaseButtonStyle}"
VerticalAlignment="Top"
Width="400"
Height="220" />
//ShowImage is being called via DispatcherTimer every 5 seconds
private void ShowImage(int p, int b)
{
if (p < 0 || p > Model.HomeCarouselItems.Count - 1) return;
var filePath = //FilePath
if (File.Exists(filePath) && !Utils.IsFileLocked(new FileInfo(filePath)))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri(filePath,UriKind.Absolute);
bitmapImage.EndInit();
bitmapImage.Freeze();
switch (b)
{
case 1:
leftImage.Background = new ImageBrush() { ImageSource = bitmapImage };
break;
case 3:
rightImage.Background = new ImageBrush() { ImageSource = bitmapImage };
break;
case 2:
center.Content = new Image() { Source = bitmapImage };
break;
}
}
}
有人可以看看并帮助我理解我在这里做错了什么..