积极答复者
Silverlight的Animation问题

问题
-
我尝试了MSDN的一段Code,但是没有移动的动画效果,找不出问题,请expert帮忙,我用的是silverlight 2.0 beta
Code SnippetPage.xaml:
<
UserControl x:Class="SilverlightApplicationTest.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded"> </Grid></
UserControl>Page.xaml.cs
public
partial class Page : UserControl{
public Page(){
InitializeComponent();
}
{
Rectangle myRectangle = new Rectangle();myRectangle.Width = 20;
myRectangle.Height = 20;
Color myColor = Color.FromArgb(255, 255, 0, 0); SolidColorBrush myBrush = new SolidColorBrush(myColor);myRectangle.Fill = myBrush;
LayoutRoot.Children.Add(myRectangle);
Duration duration = new Duration(TimeSpan.FromSeconds(0.2)); DoubleAnimation myDoubleAnimationTop = new DoubleAnimation(); DoubleAnimation myDoubleAnimationLeft = new DoubleAnimation();myDoubleAnimationTop.Duration = duration;
myDoubleAnimationLeft.Duration = duration;
myDoubleAnimationLeft.RepeatBehavior =
RepeatBehavior.Forever;myDoubleAnimationTop.RepeatBehavior =
RepeatBehavior.Forever;myDoubleAnimationLeft.AutoReverse =
true;myDoubleAnimationTop.AutoReverse =
true; Storyboard sb = new Storyboard();sb.Duration = duration;
sb.Children.Add(myDoubleAnimationLeft);
sb.Children.Add(myDoubleAnimationTop);
Storyboard.SetTarget(myDoubleAnimationLeft, myRectangle); Storyboard.SetTarget(myDoubleAnimationTop, myRectangle); //Storyboard.SetTargetName(myDoubleAnimationLeft, "myRectangle"); //Storyboard.SetTargetName(myDoubleAnimationTop, "myRectangle"); Storyboard.SetTargetProperty(myDoubleAnimationTop, new PropertyPath("(Canvas.Top)")); Storyboard.SetTargetProperty(myDoubleAnimationLeft, new PropertyPath("(Canvas.Left)"));myDoubleAnimationLeft.To = 200;
myDoubleAnimationTop.To = 200;
LayoutRoot.Resources.Add(
"unique_id", sb);sb.Begin();
}
}
答案
-
为了使动画明显,可以改成这样
Code Snippet<UserControl x:Class="SilverlightAmation.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid Background="White">
<Canvas x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded"></Canvas>
</Grid>
</UserControl>Rectangle myRectangle = new Rectangle();
myRectangle.Width = 20;
myRectangle.Height = 20;
Color myColor = Color.FromArgb(255, 255, 0, 0);
SolidColorBrush myBrush = new SolidColorBrush(myColor);
myRectangle.Fill = myBrush;
LayoutRoot.Children.Add(myRectangle);
Duration duration = new Duration(TimeSpan.FromSeconds(0.2));
DoubleAnimation myDoubleAnimationTop = new DoubleAnimation();
DoubleAnimation myDoubleAnimationLeft = new DoubleAnimation();
myDoubleAnimationTop.Duration = duration;
myDoubleAnimationLeft.Duration = duration;
myDoubleAnimationLeft.RepeatBehavior = RepeatBehavior.Forever;
myDoubleAnimationTop.RepeatBehavior = RepeatBehavior.Forever;
myDoubleAnimationLeft.AutoReverse = true;
myDoubleAnimationTop.AutoReverse = true;
Storyboard sb = new Storyboard();
sb.Duration = duration;
sb.Children.Add(myDoubleAnimationLeft);
sb.Children.Add(myDoubleAnimationTop);
Storyboard.SetTarget(myDoubleAnimationLeft, myRectangle);
Storyboard.SetTarget(myDoubleAnimationTop, myRectangle);
Storyboard.SetTargetProperty(myDoubleAnimationTop, new PropertyPath("(Canvas.Top)"));
Storyboard.SetTargetProperty(myDoubleAnimationLeft, new PropertyPath("(Canvas.Left)"));
myDoubleAnimationLeft.To = 200;
myDoubleAnimationTop.To = 200;
LayoutRoot.Resources.Add("unique_id", sb);
sb.AutoReverse = true;
sb.Duration = new Duration(TimeSpan.FromSeconds(0.2));
sb.RepeatBehavior = RepeatBehavior.Forever;
sb.Begin();
全部回复
-
改成这样既可
Code Snippet<UserControl x:Class="SilverlightAmation.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid Background="White">
<Canvas x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded"></Canvas>
</Grid>
</UserControl>
-
为了使动画明显,可以改成这样
Code Snippet<UserControl x:Class="SilverlightAmation.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid Background="White">
<Canvas x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded"></Canvas>
</Grid>
</UserControl>Rectangle myRectangle = new Rectangle();
myRectangle.Width = 20;
myRectangle.Height = 20;
Color myColor = Color.FromArgb(255, 255, 0, 0);
SolidColorBrush myBrush = new SolidColorBrush(myColor);
myRectangle.Fill = myBrush;
LayoutRoot.Children.Add(myRectangle);
Duration duration = new Duration(TimeSpan.FromSeconds(0.2));
DoubleAnimation myDoubleAnimationTop = new DoubleAnimation();
DoubleAnimation myDoubleAnimationLeft = new DoubleAnimation();
myDoubleAnimationTop.Duration = duration;
myDoubleAnimationLeft.Duration = duration;
myDoubleAnimationLeft.RepeatBehavior = RepeatBehavior.Forever;
myDoubleAnimationTop.RepeatBehavior = RepeatBehavior.Forever;
myDoubleAnimationLeft.AutoReverse = true;
myDoubleAnimationTop.AutoReverse = true;
Storyboard sb = new Storyboard();
sb.Duration = duration;
sb.Children.Add(myDoubleAnimationLeft);
sb.Children.Add(myDoubleAnimationTop);
Storyboard.SetTarget(myDoubleAnimationLeft, myRectangle);
Storyboard.SetTarget(myDoubleAnimationTop, myRectangle);
Storyboard.SetTargetProperty(myDoubleAnimationTop, new PropertyPath("(Canvas.Top)"));
Storyboard.SetTargetProperty(myDoubleAnimationLeft, new PropertyPath("(Canvas.Left)"));
myDoubleAnimationLeft.To = 200;
myDoubleAnimationTop.To = 200;
LayoutRoot.Resources.Add("unique_id", sb);
sb.AutoReverse = true;
sb.Duration = new Duration(TimeSpan.FromSeconds(0.2));
sb.RepeatBehavior = RepeatBehavior.Forever;
sb.Begin();