首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

设置 ScaleTransform.ScaleXProperty 动画的代码解决思路

2012-03-26 
设置 ScaleTransform.ScaleXProperty 动画的代码NameScope.SetNameScope(this, new NameScope())Button b

设置 ScaleTransform.ScaleXProperty 动画的代码
NameScope.SetNameScope(this, new NameScope());

  Button button1 = new Button();
  button1.Width = 100;
  button1.Height = 100;
  button1.Content = "fds";
  button1.Name = "button1";
  c1.Children.Add(button1);
  button1.RenderTransform = new TranslateTransform();  
  this.RegisterName(button1.Name, button1);
  Storyboard story = new Storyboard();
  story.AutoReverse = true;
  story.RepeatBehavior = RepeatBehavior.Forever;
  DoubleAnimation yAnimation = new DoubleAnimation();//子动画
  yAnimation.From = 0;
  yAnimation.To = 100;
  yAnimation.Duration = new Duration(TimeSpan.FromSeconds(10));


  DependencyProperty[] propertyChain =
  new DependencyProperty[] { Button.RenderTransformProperty,  
  TransformGroup.ChildrenProperty,
  ScaleTransform.ScaleXProperty};
  string thePath = "(0).(1)[0].(2)";
  Storyboard.SetTargetName(yAnimation, button1.Name);
  Storyboard.SetTargetProperty(yAnimation, new PropertyPath(thePath, propertyChain));
  
  story.Children.Add(yAnimation);  
  story.Begin(this);

调试时报错 {"路径“(0).(1)[0].(2)”中的“Children”属性值指向“System.Windows.Media.TransformCollection”的不可变实例。"}

[解决办法]
修改:

public class FlashBulb : Effect {

protected override ProceduralAnimation CreateEffectAnimation(EffectDirection direction) {
FrameworkElement target = GetTarget();

ScaleTransform scaleTransform = target.RenderTransform as ScaleTransform;
if (scaleTransform == null) {
scaleTransform = new ScaleTransform();
target.RenderTransform = scaleTransform;
target.RenderTransformOrigin = new Point(0.5, 0.5);
}

TimeSpan duration = TimeSpan.FromMilliseconds(Duration.TotalMilliseconds / 2);
ProceduralAnimationEasingFunction easing = GetEasingFunction();

 
DoubleAnimation opacityAnimation =
new DoubleAnimation(target, UIElement.OpacityProperty, duration, 0.25);
DoubleAnimation scaleXAnimation =
new DoubleAnimation(scaleTransform, ScaleTransform.ScaleXProperty, duration, 1.1);
DoubleAnimation scaleYAnimation =
new DoubleAnimation(scaleTransform, ScaleTransform.ScaleYProperty, duration, 1.1);

opacityAnimation.EasingFunction = easing;
opacityAnimation.AutoReverse = true;

scaleXAnimation.EasingFunction = easing;
scaleXAnimation.AutoReverse = true;

scaleYAnimation.EasingFunction = easing;
scaleYAnimation.AutoReverse = true;


return new ProceduralAnimationSet(opacityAnimation, scaleXAnimation, scaleYAnimation);
}
}
[解决办法]
参考一下这个写法
private void Create_And_Run_Animation(object sender, EventArgs e)
{
// Create a red rectangle that will be the target
// of the animation.
Rectangle myRectangle = new Rectangle();
myRectangle.Width = 200;
myRectangle.Height = 200;
Color myColor = Color.FromArgb(255, 255, 0, 0);
SolidColorBrush myBrush = new SolidColorBrush();
myBrush.Color = myColor;
myRectangle.Fill = myBrush;



// Add the rectangle to the tree.
LayoutRoot.Children.Add(myRectangle);

// Create a duration of 2 seconds.
Duration duration = new Duration(TimeSpan.FromSeconds(2));

// Create two DoubleAnimations and set their properties.
DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();

myDoubleAnimation1.Duration = duration;
myDoubleAnimation2.Duration = duration;

Storyboard sb = new Storyboard();
sb.Duration = duration;

sb.Children.Add(myDoubleAnimation1);
sb.Children.Add(myDoubleAnimation2);

Storyboard.SetTarget(myDoubleAnimation1, myRectangle);
Storyboard.SetTarget(myDoubleAnimation2, myRectangle);

// 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("(Canvas.Left)"));
Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath("(Canvas.Top)"));

myDoubleAnimation1.To = 200;
myDoubleAnimation2.To = 200;

// Make the Storyboard a resource.
LayoutRoot.Resources.Add("unique_id", sb);

// Begin the animation.
sb.Begin();
}

[解决办法]
楼主目的是想将XAML代码迁移至后台CS中,这两个差别还是比较大的,可以尝试执行下列代码(已在SL3.0测试过)

C# code
     Button myButton;        void MainPage_Loaded(object sender, RoutedEventArgs e)        {            myButton = new Button();            myButton.Height = 25;            myButton.Width = 100;            myButton.Content = "Click me";            //myButton.RenderTransformOrigin = new Point(10,10);            ScaleTransform myScTransform = new ScaleTransform();            TransformGroup myTransGroup = new TransformGroup();    //要在后面的代码中使用必须先进行注册            myTransGroup.Children.Add(myScTransform);            myButton.RenderTransform = myTransGroup;            myButton.Click += new RoutedEventHandler(myButton_Click);            LayoutRoot.Children.Add(myButton);        }        void myButton_Click(object sender, RoutedEventArgs e)        {            Storyboard story = new Storyboard();            DoubleAnimation yAnimation = new DoubleAnimation();            yAnimation.From = 0.5;            yAnimation.To = 1.0;            yAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));            ScaleTransform transY = (ScaleTransform)((TransformGroup)myButton.RenderTransform).Children[0]; //找到你注册的属性            Storyboard.SetTarget(yAnimation, transY);            Storyboard.SetTargetProperty(yAnimation, new PropertyPath(ScaleTransform.ScaleYProperty));                        story.Children.Add(yAnimation);            story.Begin();        } 

热点排行