我正在测试Storyboard.SetTarget Method。我需要对矩形的宽度和高度变化进行动画处理。 我找到了Microsoft示例,但是当我在程序中包含代码时,解决方案不会生成和部署。
我收到这样的错误:
IDE0006 加载项目时遇到错误。一些项目 功能,例如失败项目的完整解决方案分析,以及 依赖于它的项目已被禁用。
错误 CS0246:找不到类型或命名空间名称"颜色"(是否缺少 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 示例:
情节提要类 它现在可以工作了,但它改变了矩形的位置而不是宽度。 如何更改要应用于矩形的宽度属性的过渡?
新代码如下所示:
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 中正确获得矩形.宽度属性。 智能 说:
无法从"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;
有关更多信息,请参阅故事板动画官方文档。