Windows 8 - BeginAnimation? - c#

It seems I can't do myObject.BeginAnimation(dp , animation).
Is this a bug or has it been changed?

You need to use a storyboard. Add your animation to the storyboard and have the storyboard begin the animation.
var storyboard = new Storyboard();
var opacityAnimation = new DoubleAnimation {
From = 0,
To = 1,
Duration = DurationHelper.FromTimeSpan(TimeSpan.FromSeconds(1)),
};
storyboard.Children.Add(opacityAnimation);
Storyboard.SetTargetProperty(opacityAnimation, "Opacity");
Storyboard.SetTarget(storyboard, myObject);
storyboard.Begin();

Related

Change position of Grid using animation C#

WPF
<Grid Name="GhostGrid">
<Image Source="img.jpg"/>
</Grid>
I am trying to change the position of a grid using animation. This is working fine -
C#
TranslateTransform transformImage = new TranslateTransform();
GhostGrid.RenderTransform = transformImage;
DoubleAnimation animationImage = new DoubleAnimation(0, -50, TimeSpan.FromSeconds(0.6));
transformImage.BeginAnimation(TranslateTransform.XProperty, animationImage);
But this not working -
C#
DoubleAnimation animationImage = new DoubleAnimation()
{
From = 0,
To = -450,
Duration = new Duration(TimeSpan.FromSeconds(0.8))
};
Storyboard.SetTarget(animationImage, GhostGrid);
Storyboard.SetTargetProperty(animationImage,new PropertyPath(TranslateTransform.XProperty));
storyBoard = new Storyboard();
storyBoard.Children.Add(animationImage);
storyBoard.Begin();
As per Clemens suggestion its working now -
DoubleAnimation animationImage = new DoubleAnimation()
{
From = 0,
To = -450,
Duration = new Duration(TimeSpan.FromSeconds(0.8))
};
Storyboard.SetTarget(animationImage, GhostGrid);
Storyboard.SetTargetProperty(animationImage,new PropertyPath("RenderTransform.X"));
storyBoard = new Storyboard();
storyBoard.Children.Add(animationImage);
storyBoard.Begin();

Animation for several ui elements in UWP

This is my code to do animation (change opacity) for one ui element.
var animation = new DoubleAnimation
{
To = 0.0,
Duration = TimeSpan.FromSeconds(5),
FillBehavior = FillBehavior.HoldEnd
};
Storyboard story = new Storyboard();
Storyboard.SetTarget(animation, element1);
Storyboard.SetTargetProperty(animation, "Opacity");
story.Children.Add(animation);
story.Begin();
It works, for some reason I need to it only programmatically. The problem is I need to animate several controls at once. Is there any solution for several controls?
You would have to define several animations for this controls.
var animation1 = new DoubleAnimation
{
To = 0.0,
Duration = TimeSpan.FromSeconds(5),
FillBehavior = FillBehavior.HoldEnd
};
var animation2 = new DoubleAnimation
{
To = 0.0,
Duration = TimeSpan.FromSeconds(5),
FillBehavior = FillBehavior.HoldEnd
};
Storyboard.SetTarget(animation1, element1);
Storyboard.SetTargetProperty(animation1, "Opacity");
Storyboard.SetTarget(animation2, element2);
Storyboard.SetTargetProperty(animation2, "Opacity");
Storyboard story = new Storyboard();
story.Children.Add(animation1);
story.Children.Add(animation2);
story.Begin();

Trouble Creating a Double Animation

I am creating a flashing animation for a Text Block in WPF in the code behind. I know that the text block as the opacity property, yet this code cannot seem to find it. What am I doing wrong?
DoubleAnimation da = new DoubleAnimation
{
From = 1,
To = 0,
Duration = new Duration(TimeSpan.FromSeconds(0.5)),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
myTextBlock.BeginAnimation(Opacity, da);
In addition, I am trying to create a color animation. That doesn't seem to be working either:
ColorAnimation ca = new ColorAnimation
{
From = Colors.Red,
To = Colors.Black,
Duration = new Duration(TimeSpan.FromSeconds(1)),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
myTextBlock.Background.BeginAnimation(SolidColorBrush.ColorProperty, ca);
Animation is applied to Property, not to Field. Also it seem you use parent's Opacity, not the TextBlock.
Change Opacity to UIElement.OpacityProperty:
DoubleAnimation da = new DoubleAnimation
{
From = 1,
To = 0,
Duration = new Duration(TimeSpan.FromSeconds(0.5)),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
myTextBlock.BeginAnimation(UIElement.OpacityProperty, da);
Storyboard sboard = new Storyboard();
DoubleAnimation da = new DoubleAnimation
{
From = 1,
To = 0,
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
Storyboard.SetTarget(da, urButton);
Storyboard.SetTargetProperty(animation, new PropertyPath((object)UIElement.OpacityProperty));
sboard.Children.Add(animation);
sboard.Start()
Your code works for me. Did you set the background color to red, first? If not try setting the background color to red.

Animating drop shadow fade-in/fade-out with a storyboard

I'd like to fade in a drop shadow effect on a DataGrid over 2 seconds, which fades out over 2 seconds again after the fade-in animation is completed.
My code so far:
DropShadowEffect dropShadowEffect = new DropShadowEffect();
dropShadowEffect.ShadowDepth = 0;
dropShadowEffect.Color = Colors.LightSeaGreen;
dropShadowEffect.Opacity = 0;
dropShadowEffect.BlurRadius = 20;
element.Effect = dropShadowEffect;
Storyboard storyboard1 = new Storyboard();
TimeSpan duration1 = TimeSpan.FromMilliseconds(2000);
DoubleAnimation animateOpacity1 = new DoubleAnimation() { From = 0, To = 1, Duration = new Duration(duration1) };
Storyboard.SetTargetName(animateOpacity1, element.Name);
Storyboard.SetTargetProperty(animateOpacity1, new PropertyPath(DropShadowEffect.OpacityProperty));
DoubleAnimation animateOpacity2 = new DoubleAnimation() { From = 1, To = 0, Duration = new Duration(duration1) };
Storyboard.SetTargetName(animateOpacity2, element.Name);
Storyboard.SetTargetProperty(animateOpacity2, new PropertyPath(DropShadowEffect.OpacityProperty));
storyboard1.Children.Add(animateOpacity1);
storyboard1.Children.Add(animateOpacity2);
storyboard1.Begin(element);
Upon executing the code, nothing happens.
If you simply want to do DoubleAnimation, no need to complex it using StoryBoard. Also, you can achieve this with only single double animation with property AutoReverse set to true.
Moreover, do animation on dropShadowEffect object instead of element object.
TimeSpan duration = TimeSpan.FromMilliseconds(2000);
DoubleAnimation animateOpacity = new DoubleAnimation() { From = 0, To = 1,
Duration = duration, AutoReverse = true };
dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty,
animateOpacity);

How can I animate the Margin of a Stackpanel with a storyboard?

I would like to use it, but it doesnt't work, I wanna create a tile animation, in code behind, or if you know a project for this gol, pls write me
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
while(true){
Duration duration = new Duration(TimeSpan.FromSeconds(0.15));
// Create two DoubleAnimations and set their properties.
DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
myDoubleAnimation1.Duration = duration;
myDoubleAnimation1.From = -173
myDoubleAnimation1.To = 173;
Storyboard sb = new Storyboard();
sb.Duration = duration;
sb.Children.Add(myDoubleAnimation1);
Storyboard.SetTarget(myDoubleAnimation1, image);
// Set the attached properties of Canvas.Left and Canvas.Top
// to be the target properties of the two respective DoubleAnimations.
Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath(StackPanel.MarginProperty));
// Begin the animation.
sb.Begin();}
});
Here is the working example, if anybody needs:
//Animate margin for Label, named "label" from right to left. from 300 to 0.
var sb = new Storyboard();
var ta = new ThicknessAnimation();
ta.BeginTime = new TimeSpan(0);
ta.SetValue(Storyboard.TargetNameProperty, "label");
Storyboard.SetTargetProperty(ta, new PropertyPath(MarginProperty));
ta.From = new Thickness(300, 30, 0, 0);
ta.To = new Thickness(0, 30, 0, 0);
ta.Duration = new Duration(TimeSpan.FromSeconds(3));
sb.Children.Add(ta);
sb.Begin(this);
Use a ThicknessAnimation instead of a DoubleAnimation. It's almost the same.
Edit:
If you want to make the Animation endless use Timeline.RepeatBehavior.
myThicknessAnimation1.RepeatBehavior = RepeatBehavior.Forever;

Categories

Resources