使用代码C#UWP

时间:2018-03-15 10:06:32

标签: c# animation uwp storyboard doubleanimation

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

我得到这样的错误:

  

IDE0006加载项目时遇到错误。一些项目   功能,例如失败项目的完整解决方案分析和   依赖它的项目已被禁用。

     

错误CS0246:类型或命名空间名称'颜色'找不到(你错过了使用指令或汇编引用吗?)

当我添加名称空间时:

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样本:

Storyboard Class 它现在有效,但它改变了矩形的位置而不是宽度。 如何更改要应用于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上找到资源。

1 个答案:

答案 0 :(得分:1)

  

无法从'Windows.UI.Xaml.DependencyProperty'转换为'string'

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

Width

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

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);

有关详情,请参阅Storyboarded animations官方文档。