Answered by:
70-511 Microsoft Press: Practice Slideshow Application

Question
-
Hi
I'm preparing myself for the 70-511 exam. I bought the book "MCTS Self-Paced Training Kit (Exam 70-511): Windows Applications Development with Microsoft .NET Framework 4" and I have the following question:
At the end of the chapter 2 Lesson 3 "Implementing Animation" as suggested practice is the creation of a slideshow application which reads images from a folder and display each image for 10 seconds before switching automatically to the next one.
I tried to implement this, but I did find any way to change the source for the image control using animation storyboard:
My WPF image control:
<Image Name="objImage" DockPanel.Dock="Top"> </Image>
The code excuting the comand "start slideshow":
Private Sub StartSlideshow(sender As Object, e As ExecutedRoutedEventArgs) Dim objDirDlg As New System.Windows.Forms.FolderBrowserDialog objDirDlg.Description = "Select the Images Folder" objDirDlg.RootFolder = System.Environment.SpecialFolder.Desktop objDirDlg.SelectedPath = System.Environment.SpecialFolder.MyPictures objDirDlg.ShowNewFolderButton = False If objDirDlg.ShowDialog() <> Forms.DialogResult.OK Then Exit Sub End If Dim strDir As String = objDirDlg.SelectedPath MessageBox.Show(String.Format("Start scanning folder {0} for images...", strDir)) Dim astrImg As New List(Of String) For Each strFile As String In Directory.GetFiles(strDir) Dim objFile As New FileInfo(strFile) Select Case objFile.Extension.ToLower Case ".jpeg", ".jpg", ".bmp", ".gif", ".png" astrImg.Add(strFile) End Select Next Dim objAni As New Animation.StringAnimationUsingKeyFrames() For i As Integer = 0 To astrImg.Count - 1 Dim objAniKey As New Animation.DiscreteStringKeyFrame(astrImg.ElementAt(i)) objAniKey.KeyTime = New TimeSpan(0, 0, 10 * i) objAni.KeyFrames.Add(objAniKey) Next objImage.BeginAnimation(Image.SourceProperty, objAni) End Sub
I thought I could use a StringAnimationUsingKeyFrames where each key frame has as value the path to the picture and give as property to change for the animation the Source property of the image. Unfortunately if I run the application I receive the following error:
AnimationTimeline of type 'System.Windows.Media.Animation.StringAnimationUsingKeyFrames' cannot be used to animate the 'Source' property of type 'System.Windows.Media.ImageSource'.
If I replace the image control with a label and set the property Content for the animation it runs correctly.
What is the correct solution to change the source property of the image control using animation ? How do I have to use another control as the Image control ?Thank you for any help
Albert
Friday, April 15, 2011 5:41 PM
Answers
-
Hi,
use the class System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames and System.Windows.Media.Animation.DiscreteObjectKeyFrame.
Sara.
- Marked as answer by Rubel Khan Wednesday, June 8, 2011 6:22 AM
Wednesday, May 11, 2011 1:53 PM
All replies
-
Hi,
use the class System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames and System.Windows.Media.Animation.DiscreteObjectKeyFrame.
Sara.
- Marked as answer by Rubel Khan Wednesday, June 8, 2011 6:22 AM
Wednesday, May 11, 2011 1:53 PM -
Hi Sara
Thank you for your answer. With the following code it now works except when I select a path with more than 3 pictures. The animation does display only the first 2 pictures. I added also an animation with the filename of the picture and this animation is working. I tought it may be because my pictures are too big, so I reduced the size of all pictures and now it display the first 5 pictures. I then reduced the number of pictures and then it display the first 10 pictures...
This is very strange. Is there any limitation about the size of the ObjectAnimationUsingKeyFrames collection? How is the way I load the picture wrong ?
Dim objAniPict As New Media.Animation.ObjectAnimationUsingKeyFrames For i As Integer = 0 To astrImg.Count - 1 Dim objPict As New BitmapImage(New Uri(astrImg(i))) objPict.CacheOption = BitmapCacheOption.None Dim objAniKey As New Media.Animation.DiscreteObjectKeyFrame(objPict) objAniKey.KeyTime = New TimeSpan(0, 0, 1 * i) objAniPict.KeyFrames.Add(objAniKey) Debug.WriteLine(String.Format("Image {0}: {1}", i, astrImg(i))) Next Dim objAni As New Animation.StringAnimationUsingKeyFrames() For i As Integer = 0 To astrImg.Count - 1 Dim objAniKey As New Animation.DiscreteStringKeyFrame(astrImg.ElementAt(i)) objAniKey.KeyTime = New TimeSpan(0, 0, 1 * i) objAni.KeyFrames.Add(objAniKey) Next objImgBrush.BeginAnimation(ImageBrush.ImageSourceProperty, objAniPict) objLabel.BeginAnimation(Label.ContentProperty, objAni)
Thank you very much for your help
Albert
Tuesday, May 17, 2011 9:07 PM -
Hello,
I think the problem may be the duration of the animation.
Try this:objAniPict.Duration = New Duration(new TimeSpan(0, 0, (files.Length *2)));
I show my code:
System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames aAnimation = new System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames();
System.Windows.Media.Animation.DiscreteObjectKeyFrame objAniKey;aAnimation.Duration = new Duration(new TimeSpan(0, 0, (files.Length *2)));
for (int i = 0; i < files.Length; i++)
{
objAniKey = new System.Windows.Media.Animation.DiscreteObjectKeyFrame();
objAniKey.KeyTime = new TimeSpan(0, 0, 2 * i);
objAniKey.Value = ObtenerImage(i);
aAnimation.KeyFrames.Add(objAniKey);
}aAnimation.FillBehavior = System.Windows.Media.Animation.FillBehavior.Stop;
image.BeginAnimation(Image.SourceProperty, aAnimation);
Sara.Monday, May 23, 2011 12:33 PM -
I had the same problem doing the same practice. But probably you need to declare the animation as a private member. Then you can have as many images as you want.
private ObjectAnimationUsingKeyFrames _animation;
Probably the animation goes out of scope and stopped when the CG destroyed the animation.
Monday, October 8, 2012 7:55 PM