Animation problem
-
I use animations all the time in WPF, but they seem to work completely different in Silverlight, principally because the elements being animated don't have a BeginAnimation method. So, given the element being animated, how can I find the Storyboard defining the animation?
-
I use animations all the time in WPF, but they seem to work completely different in Silverlight, principally because the elements being animated don't have a BeginAnimation method. So, given the element being animated, how can I find the Storyboard defining the animation?
-
I thought it was clear that I had created my Storyboard in XAML. This is what I had before I made my first post:
navigation:Page.Resources
<Storyboard x:Name="image01fadeIn">
<DoubleAnimation
Storyboard.TargetName="image01"
Storyboard.TargetProperty="Opacity"
From="0" To="1"
Duration="0:0:0.5"
Completed="imag01FadeInAnimation_Completed"
>
</DoubleAnimation>
</Storyboard>
</navigation:Page.Resources>Now I'm getting an event where I have a reference to the element I want to start the above animation on. Given the reference to that the element (image01), how do I find the above Storyboard so I can start the animation? It was simple in WPF, because I could create the animation in code and use BeginAnimation() right there in the code. But in Silverlight, apparently you have to create the Storyboard in a XAML resource. Maybe my question should be: how would I replace the above XAML with in-line C# code?
-
I thought it was clear that I had created my Storyboard in XAML. This is what I had before I made my first post:
navigation:Page.Resources
<Storyboard x:Name="image01fadeIn">
<DoubleAnimation
Storyboard.TargetName="image01"
Storyboard.TargetProperty="Opacity"
From="0" To="1"
Duration="0:0:0.5"
Completed="imag01FadeInAnimation_Completed"
>
</DoubleAnimation>
</Storyboard>
</navigation:Page.Resources>Now I'm getting an event where I have a reference to the element I want to start the above animation on. Given the reference to that the element (image01), how do I find the above Storyboard so I can start the animation? It was simple in WPF, because I could create the animation in code and use BeginAnimation() right there in the code. But in Silverlight, apparently you have to create the Storyboard in a XAML resource. Maybe my question should be: how would I replace the above XAML with in-line C# code?
Your code in C# will look something very similar to -
Storyboard sb = new Storyboard(); DoubleAnimation objAnimation = new DoubleAnimation(); objAnimation.From = 0; objAnimation.To = 1; objAnimation.Duration = new Duration(new TimeSpan(500)); objAnimation.Completed +=new EventHandler(imag01FadeInAnimation_Completed); Storyboard.SetTargetProperty(objAnimation, new PropertyPath("(Opacity)")); Storyboard.SetTarget(objAnimation, image1); sb.Children.Add(objAnimation); sb.Begin();
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
Honestly. It's the honest ones you want to watch out for... -
Your code in C# will look something very similar to -
Storyboard sb = new Storyboard(); DoubleAnimation objAnimation = new DoubleAnimation(); objAnimation.From = 0; objAnimation.To = 1; objAnimation.Duration = new Duration(new TimeSpan(500)); objAnimation.Completed +=new EventHandler(imag01FadeInAnimation_Completed); Storyboard.SetTargetProperty(objAnimation, new PropertyPath("(Opacity)")); Storyboard.SetTarget(objAnimation, image1); sb.Children.Add(objAnimation); sb.Begin();
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
Honestly. It's the honest ones you want to watch out for...Exactly. Just after I posted my reply, I woke up and banged the side of my head with the palm of my hand. I work too many hours in the day and my brain stops working after about 12 hours of headscratching at my desk. My original post was written after 16 hours at the keyboard. Then when I woke up this morning I was still thinking I didn't have a clue. But my brain started waking up at the very end to my reply to your first answer. Then I sat down and wrote the following code (amazingly similar to yours):
ImageBeingDisplayed.Source = BitmapImage;
DoubleAnimation fadeInAnimation = new DoubleAnimation();
fadeInAnimation.From = 0;
fadeInAnimation.To = 1;
fadeInAnimation.Duration = TimeSpan.FromSeconds(.5);
fadeInAnimation.Completed += new EventHandler(fadeInAnimation_Completed);
Storyboard storyboard = new Storyboard();
Storyboard.SetTarget(fadeInAnimation, ImageBeingDisplayed);
Storyboard.SetTargetProperty(fadeInAnimation, new PropertyPath("Image.Opacity"));
storyboard.Children.Add(fadeInAnimation);
storyboard.Begin();Then I wrote the
fadeInAnimation_Completed
event as follows:void fadeInAnimation_Completed(object sender, EventArgs e)
{
Storyboard storyboard = (Storyboard)sender;
storyboard.Stop();
string imageName = Storyboard.GetTargetName(storyboard);
Image image = FindName(imageName) as Image;
if (image != null)
{
image.Opacity = 1;
}
}I don't know whether all this will work because the first block of code is in a WebClient.DownloadDataCompleted event handler, which inexplicably does not exist in Silverlight (I'm trying to port my WPF application to Silverlight), so my Silverlight code does not compile. I asked how to solve that problem in a post immediately preceding the post that started this thread.