图像过渡
我又被卡住了。
我正在用 C# 创建一个 WPF 项目...
我有一个图像(相册封面),我将在后面的代码中对其进行更改,并且希望基本上执行以下操作。
当确定并获取新的专辑封面图像时,我希望图像控件中的当前图像淡出,使用新封面进行更新,然后淡入。
我没有看到很多关于如何完成的好例子这在代码后面。
以下是我最近失败的尝试...
if (currentTrack != previousTrack)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(Address, UriKind.Absolute);
image.EndInit();
Storyboard MyStoryboard = new Storyboard();
DoubleAnimation FadeOut = new DoubleAnimation();
FadeOut.From = 1.0;
FadeOut.To = 0.0;
FadeOut.Duration = new Duration(TimeSpan.FromSeconds(.5));
MyStoryboard.Children.Add(FadeOut);
Storyboard.SetTargetName(FadeOut, CoverArt.Name);
Storyboard.SetTargetProperty(FadeOut, new PropertyPath(Rectangle.OpacityProperty));
CoverArt.Source = image;
DoubleAnimation Fadein = new DoubleAnimation();
Fadein.From = 0.0;
Fadein.To = 1.0;
Fadein.Duration = new Duration(TimeSpan.FromSeconds(.5));
MyStoryboard.Children.Add(Fadein);
Storyboard.SetTargetName(Fadein, CoverArt.Name);
Storyboard.SetTargetProperty(Fadein, new PropertyPath(Rectangle.OpacityProperty));
MyStoryboard.Begin(this);
}
我更愿意在代码后面执行此操作,因为这就是我获取图像的地方。否则,我不确定如何触发它。
一个例子将不胜感激。
I've gotten stuck again.
I'm creating a WPF project in C#...
I've got an image (album cover) that I'll be changing in code behind, and wish to basicaly do the following.
When a new album cover image is determine and aquired, I want the current image in the image control to fade out, get updated with the new cover, and then fade back in.
I'm not seeing very many good examples on how to accomplish this in code behind.
The following was my latest failed attempt...
if (currentTrack != previousTrack)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = new Uri(Address, UriKind.Absolute);
image.EndInit();
Storyboard MyStoryboard = new Storyboard();
DoubleAnimation FadeOut = new DoubleAnimation();
FadeOut.From = 1.0;
FadeOut.To = 0.0;
FadeOut.Duration = new Duration(TimeSpan.FromSeconds(.5));
MyStoryboard.Children.Add(FadeOut);
Storyboard.SetTargetName(FadeOut, CoverArt.Name);
Storyboard.SetTargetProperty(FadeOut, new PropertyPath(Rectangle.OpacityProperty));
CoverArt.Source = image;
DoubleAnimation Fadein = new DoubleAnimation();
Fadein.From = 0.0;
Fadein.To = 1.0;
Fadein.Duration = new Duration(TimeSpan.FromSeconds(.5));
MyStoryboard.Children.Add(Fadein);
Storyboard.SetTargetName(Fadein, CoverArt.Name);
Storyboard.SetTargetProperty(Fadein, new PropertyPath(Rectangle.OpacityProperty));
MyStoryboard.Begin(this);
}
I'd prefer to do this in code behind simply because that is where I'm aquiring the image. Otherwise, I'm not sure how I'd trigger it.
An example would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望使用相同的图像目标使旧图像淡出,然后新图像淡入,则必须等待第一个故事板完成,然后才能更改图像并开始新图像。为此,请使用
Storyboard.Completed
事件。另一方面,如果您希望旧图像淡出而新图像淡入,则需要更改设计以包含占据相同屏幕空间的两个图像并更改情节提要使用
ParallelTimeline
。If you want the old image to fade out and then the new one to fade in using the same image target, then you have to wait for the first storyboard to complete before you change the image and start the new one. To do this use the
Storyboard.Completed
event.On the other hand, if you want to the old image to fade out while the new image is fading in, you need to change your design to include two images that occupy the same screen space and change your storyboard to use a
ParallelTimeline
.能够弄清楚你的意思瑞克。我确实使用下面的代码使图像淡出、替换和淡入......
was able to figure out what you meant Rick. I do have the image fading out, replaced, and fading in with the code below....