这几天写代码的时候一直被一个奇怪的现象困扰:明明是看起来没有任何问题的C#代码,目的是实现在程序中水平移动一个按钮。代码是如此写的:

            Storyboard s = new Storyboard();
            DoubleAnimation da = new DoubleAnimation();
            da.To = 100;
            da.Duration = TimeSpan.FromSeconds(0.1);
            Storyboard.SetTarget(da, t);  //此处t为TranslateTransform对象
            Storyboard.SetTargetProperty(da,new PropertyPath(TranslateTransform.XProperty));
            s.Children.Add(da);
            s.Begin();


想破脑袋也没发现那里不对.于是我将这段代码转换成xaml:

        <Storyboard x:Key="storyboard">
            <DoubleAnimation Storyboard.TargetName="t" Storyboard.TargetProperty="X"
                             Duration="00:00:0.1"  To="100" />
        </Storyboard>
 


这样便运行起来了,但使用xaml并不能够达到目的.后来,经过漫长寻找答案的过程,终于得出了结果,代码应当是这么写:

            Storyboard s = new Storyboard();
            DoubleAnimation da = new DoubleAnimation();
            da.To = 100;
            da.Duration = TimeSpan.FromSeconds(0.1);
            Storyboard.SetTarget(da, b);//此处b为Button对象
            Storyboard.SetTargetProperty(da, new PropertyPath("RenderTransform.X"));
            s.Children.Add(da);
            s.Begin();

 


至于原因,msdn Library上已经阐述的很清楚了:

http://msdn.microsoft.com/zh-cn/library/ms742868.aspx

NameScope

 

谁让咱没好好看msdn呢?

 

就说到这儿了,高手请保持淡定,本人表示感谢.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2021-04-20
猜你喜欢
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
相关资源
相似解决方案