简体   繁体   English

WPF元素在可视化树中,但不是动画?

[英]WPF elements are in visual tree, but not animated?

I'm trying to figure out how to generate animations procedurally. 我试图弄清楚如何在程序上生成动画。 I create a Line, create four animations, and associate the line's endpoints to the animations. 我创建一个Line,创建四个动画,并将线的端点与动画相关联。 I add the animations to a storyboard, then I run the storyboard. 我将动画添加到故事板,然后我运行故事板。

The panel itself is visible (I could change the background orange, and that change shows up), but not its children. 面板本身是可见的(我可以更改背景橙色,并显示更改),但不是它的孩子。 The heck am I doing wrong? 我到底做错了什么?

Code behind: 代码背后:

using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace MyProject.Animations
{
    public class MyAnimation : Panel
    {
        private DoubleAnimation[] lineAnimation;
        private Line line;
        private Storyboard storyboard;

        public MyAnimation()
        {
            storyboard = new Storyboard();

            line = new Line();
            line.StrokeThickness = 4;
            line.Stroke = Brushes.Black;
            this.Children.Add(line);    

            lineAnimation = new DoubleAnimation[4];
            for (int i = 0; i < 4; i++)
            {
                lineAnimation[i] = new DoubleAnimation();
                lineAnimation[i].Duration = new Duration(TimeSpan.FromSeconds(5));
                lineAnimation[i].AutoReverse = true;
                lineAnimation[i].RepeatBehavior = RepeatBehavior.Forever;

                lineAnimation[i].From = 10 * i;
                lineAnimation[i].To = 100 * i;

                storyboard.Children.Add(lineAnimation[i]);
            }

            Storyboard.SetTarget(lineAnimation[0], line);
            Storyboard.SetTargetProperty(lineAnimation[0], new PropertyPath("X1"));

            Storyboard.SetTarget(lineAnimation[1], line);
            Storyboard.SetTargetProperty(lineAnimation[1], new PropertyPath("Y1"));

            Storyboard.SetTarget(lineAnimation[2], line);
            Storyboard.SetTargetProperty(lineAnimation[2], new PropertyPath("X2"));

            Storyboard.SetTarget(lineAnimation[3], line);
            Storyboard.SetTargetProperty(lineAnimation[3], new PropertyPath("Y2"));

            storyboard.Begin();
        }
    }
}

XAML: XAML:

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sdk="http://schemas.microsoft.com/netfx/2009/xaml/presentation/sdk"
        xmlns:local="clr-namespace:MyProject"
        xmlns:a="clr-namespace:Myproject.Animations"
        Title="MyProject">

    <Grid>
        <a:MyAnimation>
    </Grid>

</Window>

Inspecting the application with Snoop, I see my lines in the visual tree. 用Snoop检查应用程序,我在可视树中看到了我的行。 But they're not being drawn. 但他们并没有被吸引。 (Note, this is the debug window for the actual application, not the representative code sample I've transcribed.) (注意,这是实际应用程序的调试窗口,而不是我转录的代表性代码示例。)

When you derive from Panel , the Children are not laid out. 当你从Panel派生时,孩子们没有布置。 You would also have to override the MeasureOverride and ArrangeOverride methods. 您还必须覆盖MeasureOverrideArrangeOverride方法。 See How to: Create a Custom Panel Element for details. 有关详细信息,请参见如何:创建自定义面板元素

Or simply derive from Canvas instead: 或者简单地从Canvas派生:

public class MyAnimation : Canvas
{
    ...
}

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

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