【发布时间】:2015-03-17 09:39:44
【问题描述】:
我正在尝试自定义在 MSDN 上找到的数码相框程序模板,但我发现它最多只能显示 4 张图像。如果我将第 5 个图像添加到 ArrayList,整个屏幕将变为空白。
有谁知道发生了什么以及为什么将第 4 个图像添加到 ArrayList 后会导致图像消失和窗口空白?
[后编辑:包含完整源代码]
C#源码:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace CSWPFAnimatedImage
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
int nextImageIndex;
List<BitmapImage> images = new List<BitmapImage>();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Initialize the images collection
images.Add(new BitmapImage(new Uri("Images/image1.jpg", UriKind.Relative)));
images.Add(new BitmapImage(new Uri("Images/image2.jpg", UriKind.Relative)));
images.Add(new BitmapImage(new Uri("Images/image3.jpg", UriKind.Relative)));
images.Add(new BitmapImage(new Uri("Images/image4.jpg", UriKind.Relative)));
nextImageIndex = 2;
}
private void VisbleToInvisible_Completed(object sender, EventArgs e)
{
// Change the source of the myImage1 to the next image to be shown
// and increase the nextImageIndex
this.myImage1.Source = images[nextImageIndex++];
// If the nextImageIndex exceeds the top bound of the collection,
// get it to 0 so as to show the first image next time
if (nextImageIndex == images.Count)
{
nextImageIndex = 0;
}
// Get the InvisibleToVisible storyboard and start it
Storyboard sb = this.FindResource("InvisibleToVisible") as Storyboard;
sb.Begin(this);
}
private void InvisibleToVisible_Completed(object sender, EventArgs e)
{
// Change the source of the myImage2 to the next image to be shown
// and increase the nextImageIndex
this.myImage2.Source = images[nextImageIndex++];
// If the nextImageIndex exceeds the top bound of the collection,
// get it to 0 so as to show the first image next time
if (nextImageIndex == images.Count)
{
nextImageIndex = 0;
}
// Get the VisibleToInvisible storyboard and start it
Storyboard sb = this.FindResource("VisibleToInvisible") as Storyboard;
sb.Begin(this);
}
}
}
XAML:
<Window x:Class="CSWPFAnimatedImage.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF Animated Image Sample" Height="300" Width="300" Loaded="Window_Loaded">
<Window.Resources>
<Storyboard x:Key="VisibleToInvisible" Completed="VisbleToInvisible_Completed" >
<DoubleAnimation Storyboard.TargetName="TransparentStop"
Storyboard.TargetProperty="Offset" To="0" Duration="0:0:2" />
<DoubleAnimation Storyboard.TargetName="BlackStop"
Storyboard.TargetProperty="Offset" To="0" Duration="0:0:2"
/>
</Storyboard>
<Storyboard x:Key="InvisibleToVisible" Completed="InvisibleToVisible_Completed">
<DoubleAnimation Storyboard.TargetName="TransparentStop"
Storyboard.TargetProperty="Offset" To="1" Duration="0:0:2" />
<DoubleAnimation Storyboard.TargetName="BlackStop"
Storyboard.TargetProperty="Offset" To="1" Duration="0:0:2" />
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{StaticResource VisibleToInvisible}"/>
</EventTrigger.Actions>
</EventTrigger>
</Window.Triggers>
<Grid Name="grid">
<Image x:Name="myImage2" Source="Images/image2.jpg" />
<Image x:Name="myImage1" Source="Images/image1.jpg">
<Image.OpacityMask>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Offset="1" Color="Black" x:Name="BlackStop"/>
<GradientStop Offset="1" Color="Transparent" x:Name="TransparentStop"/>
</LinearGradientBrush>
</Image.OpacityMask>
</Image>
</Grid> </Window>
【问题讨论】:
-
我的代码运行良好
-
你的图片尺寸是多少?
-
70 KB .jpeg。尝试添加您自己的图像。该代码适用于已经存在的图像,但如果我尝试放入其他内容(这是我想要做的,因为它是数码相框),窗口就会空白。
-
我添加了自己的 8 张图片。我创建了新项目并从您提供的链接中复制了代码。对我来说没有问题。我还在 InitializeComponent() 之前移动了图像的实例化
-
我的意思是 images.add() 东西