我试图自定义我在MSDN上找到的数码相框程序模板,但我发现它最多只能显示4张图像。如果我将第五个图像添加到ArrayList,整个屏幕将变为空白。
是否有人知道发生了什么以及为什么添加到ArrayList后第4个图像会导致图像消失并且窗口变空?
[编辑后:包括完整来源]
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>
答案 0 :(得分:1)
这里有效
images.Add(new BitmapImage(new Uri("Images/image1.jpg", UriKind.Relative)));
这并不是
images.Add(new BitmapImage(new Uri("D:\\image2jpg", UriKind.Relative)));
如此。将其更改为
images.Add(new BitmapImage(new Uri("D:\\image2jpg", UriKind.Absolute)));
当您从某处复制图像并将其粘贴到项目的Images文件夹并将其添加到图像列表时,它会起作用。