繁体   English   中英

使用代码C#UWP为Rectangle的Width属性创建一个Storyboard DoubleAnimation

[英]Creating a Storyboard DoubleAnimation for Width property of Rectangle using code C# UWP

我正在测试Storyboard.SetTarget方法。 我需要为矩形的宽度和高度变化设置动画。 我找到了Microsoft示例,但是当我在程序中包含代码时,该解决方案将无法构建和部署。

我收到这样的错误:

IDE0006加载项目时遇到错误。 某些项目功能(例如针对失败项目的完整解决方案分析以及依赖该项目的项目)已被禁用。

错误CS0246:找不到类型或名称空间名称'Color'(您是否缺少using指令或程序集引用?)

当我添加名称空间时:

System.Windows.Media.Animation

我得到错误:

错误CS0234:类型或名称空间名称“媒体”在名称空间“ System.Windows”中不存在(您是否缺少程序集引用?)

我是否为UWP使用示例?

这是我使用的示例代码:

private void Create_And_Run_Animation(object sender, EventArgs e)
    {
        // Create a yellow rectangle that will be the target
        // of the animation.
        Rectangle myRectangle = new Rectangle();
        myRectangle.Width = 200;
        myRectangle.Height = 20;
        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 = 40;

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

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

好的,我正在使用Silverlight的示例。

我在这里找到了C#UWP示例:

故事板类 ,现在可以使用,但是它更改矩形的位置而不是宽度。 如何更改要应用于Rectangle的Width属性的过渡?

新代码如下:

    private void Create_And_Run_Animation()
    {

        // Create a red rectangle that will be the target
        // of the animation.
        Rectangle myRectangle = new Rectangle();
        myRectangle.Width = 20;
        myRectangle.Height = 20;
        SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);
        myRectangle.Fill = myBrush;

        // Create the transform
        TranslateTransform stretchTransform = new TranslateTransform();
        stretchTransform.X = 0;
        stretchTransform.Y = 0;
        myRectangle.RenderTransform = stretchTransform;

        // Add the rectangle to the tree.
        InfoGrid.Children.Add(myRectangle);
        myRectangle.Name = "myWidthAnimatedRectangle";
        // Create a duration of 2 seconds.
        Duration duration = new Duration(TimeSpan.FromSeconds(2));
        // Create two DoubleAnimations and set their properties.
        DoubleAnimation myDoubleAnimation = new DoubleAnimation();
        myDoubleAnimation.From = 200;
        myDoubleAnimation.To = 300;
        myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
        Storyboard justintimeStoryboard = new Storyboard();
        justintimeStoryboard.Duration = duration;
        justintimeStoryboard.Children.Add(myDoubleAnimation);

        Storyboard.SetTarget(myDoubleAnimation, stretchTransform);

        Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);
        Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Rectangle.WidthProperty));
        // Set the X and Y properties of the Transform to be the target properties
        // of the two respective DoubleAnimations.
        Storyboard.SetTargetProperty(myDoubleAnimation, "X");

        myDoubleAnimation.To = 200;


        // Make the Storyboard a resource.
        InfoGrid.Resources.Add("justintimeStoryboard", justintimeStoryboard);
        // Begin the animation.
        justintimeStoryboard.Begin();
    }

但是我无法在UWP中获得Rectangle.WidthProperty。 Intelli说:

无法从“ Windows.UI.Xaml.DependencyProperty”转换为“字符串”

我在MSDN上找不到资源。

无法从“ Windows.UI.Xaml.DependencyProperty”转换为“字符串”

问题是SetTargetProperty方法的Path参数是字符串值。 您无法将PropertyPath传递给它。 您只需要传递Width字符串值。

DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 20;
myDoubleAnimation.To = 300;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));

Storyboard.SetTarget(myDoubleAnimation, myRectangle);
Storyboard.SetTargetProperty(myDoubleAnimation, "Width");
Storyboard.SetTargetName(myDoubleAnimation, myRectangle.Name);

如果使用以下代码为属性设置动画,则不会看到任何效果。 因为您制作了一个依赖动画 默认情况下,动画系统不会运行从属动画。 您仍然可以使用此动画,但是必须专门启用每个此类从属动画。 要启用动画,请将动画对象的EnableDependentAnimation属性设置为true。

myDoubleAnimation.EnableDependentAnimation = true; 

有关更多信息,请参阅情节提要动画官方文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM