【发布时间】:2014-05-31 05:40:00
【问题描述】:
我想创建图像幻灯片。为此,我在每个设定的时间间隔后在图像控件中加载了新图像。但是每次我加载一个新图像时,它都没有任何动画,我需要为每个图像加载一个过渡动画或淡入淡出动画。如何在图像控件中更改图像时实现动画?以下是代码:
XAML:
<Grid>
<Image Source="{Binding CurrentImage}" />
</Grid>
XAML.cs
ViewModel = new ScreenSaverViewModel();
this.DataContext = ViewModel;
ViewModel.cs
/// <summary>
/// Gets the current image to display on the attract screen. Changes to this property
/// cause the PropertyChanged event to be signaled
/// </summary>
public string CurrentImage
{
get { return this.currentImage; }
protected set
{
this.currentImage = value;
this.OnPropertyChanged("CurrentImage");
}
}
/// <summary>
/// Gets the observable collection of all images.
/// </summary>
public ObservableCollection<string> Images
{
get { return this.images; }
}
public ScreenSaverViewModel()
{
images = new ObservableCollection<string>();
this.LoadSlideShowImages();
if (Images != null && Images.Count > 0)
{
this.CurrentImage = this.Images[this.currentIndex];
this.tickTimer = new DispatcherTimer();
this.tickTimer.Interval = TimeSpan.FromMilliseconds(TimerIntervalMilliseconds);
this.tickTimer.Tick += (s, e) =>
{
this.currentIndex++;
this.currentIndex = this.currentIndex < this.Images.Count ? this.currentIndex : 0;
this.CurrentImage = this.Images[this.currentIndex];
};
// start the timer after image is loaded
this.tickTimer.Start();
}
}
【问题讨论】:
-
但是我将如何为新图像的变化制作动画?意味着它应该看起来像加载了动画的新图像。
-
你可以有两个重叠的图像。淡入顶部图像,设置底部图像,淡出顶部图像,设置顶部图像,重复。 this 也可能会有所帮助。
-
我不能创建一些在图像源更改时触发的触发器吗?
标签: c# wpf image xaml animation