none
WPF用代码实现动画的问题 RRS feed

  • 问题

  • 刚刚学习WPF,按照个人的理解用代码写了一个简单的动画,但是没有动起来。是什么问题呢?

    public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public double rx { get { return (double)GetValue(rxProperty); } set { SetValue(rxProperty, value); Canvas.SetLeft(rect1, value); } } public static readonly DependencyProperty rxProperty = DependencyProperty.Register("rx", typeof(double), typeof(MainWindow), new PropertyMetadata(0.0)); private void btn1_Click(object sender, RoutedEventArgs e) { DoubleAnimation anim = new DoubleAnimation(0, 200, TimeSpan.FromSeconds(2)); BeginAnimation(rxProperty, anim); } }

    xaml:

    <StackPanel>
        <Button Name="btn1" Click="btn1_Click">click</Button>
        <Canvas>
            <Rectangle Name="rect1" Width="100" Height="50" Fill="BlueViolet"></Rectangle>
        </Canvas>
    </StackPanel>
    


    2017年2月24日 7:59

答案


  • Hi  James,

    如果你想直接对一个控件实现动态。你参考下面代码即可。

         DoubleAnimation da = new DoubleAnimation();
                da.From = double.Parse("12");
                da.To = double.Parse("160");
                da.Duration = new Duration(TimeSpan.Parse("0:0:2"));
                rect1.BeginAnimation(Rectangle.HeightProperty, da);

    我看到你实现了依赖属性。我建议你实现自定义控件或者自定义用户控件的时候创建依赖属性,然后通过双向绑定动态改变动画效果。

        public double rx
            {
                get { return (double)GetValue(rxProperty); }
                set
                {
                    SetValue(rxProperty, value);  
                }
            }
    
            public static readonly DependencyProperty rxProperty =
                DependencyProperty.Register("rx", typeof(double), typeof(MainWindow), new PropertyMetadata(0.0, 
    
    CurrentValueChange));
    
    
            public void StoryboardPlay(DependencyPropertyChangedEventArgs e)
            {
                Storyboard sb = new Storyboard();
                DoubleAnimation da = new DoubleAnimation();
                da.From = double.Parse("12");
                da.To = double.Parse(e.NewValue.ToString());
                da.Duration = new Duration(TimeSpan.Parse("0:0:2"));
                rect1.BeginAnimation(Rectangle.HeightProperty, da);
            }
    
            public static void CurrentValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                (d as donghuatest).StoryboardPlay(e);
            }
    
     <TextBox Text="{Binding ElementName=yourcontrol,Path=rx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    


    Best Regards,

    Yohann Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年2月25日 3:23
    版主

全部回复


  • Hi  James,

    如果你想直接对一个控件实现动态。你参考下面代码即可。

         DoubleAnimation da = new DoubleAnimation();
                da.From = double.Parse("12");
                da.To = double.Parse("160");
                da.Duration = new Duration(TimeSpan.Parse("0:0:2"));
                rect1.BeginAnimation(Rectangle.HeightProperty, da);

    我看到你实现了依赖属性。我建议你实现自定义控件或者自定义用户控件的时候创建依赖属性,然后通过双向绑定动态改变动画效果。

        public double rx
            {
                get { return (double)GetValue(rxProperty); }
                set
                {
                    SetValue(rxProperty, value);  
                }
            }
    
            public static readonly DependencyProperty rxProperty =
                DependencyProperty.Register("rx", typeof(double), typeof(MainWindow), new PropertyMetadata(0.0, 
    
    CurrentValueChange));
    
    
            public void StoryboardPlay(DependencyPropertyChangedEventArgs e)
            {
                Storyboard sb = new Storyboard();
                DoubleAnimation da = new DoubleAnimation();
                da.From = double.Parse("12");
                da.To = double.Parse(e.NewValue.ToString());
                da.Duration = new Duration(TimeSpan.Parse("0:0:2"));
                rect1.BeginAnimation(Rectangle.HeightProperty, da);
            }
    
            public static void CurrentValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                (d as donghuatest).StoryboardPlay(e);
            }
    
     <TextBox Text="{Binding ElementName=yourcontrol,Path=rx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
    


    Best Regards,

    Yohann Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年2月25日 3:23
    版主
  • 谢谢!非常详细!

    但我是想移动这个Rectangle,并不是想改变大小。因为Rectangle没有LeftProperty.

    2017年2月27日 1:25