wpf动画 - 鼠标按下事件不起作用

时间:2016-05-25 06:58:43

标签: c# wpf animation mousedown coloranimation

每次都有一个从窗口左侧向右移动不同距离的蓝色矩形。

点击矩形或动画完成后,矩形将从左侧再次开始移动。

如果单击矩形,它的颜色将变为绿色,持续时间为0.3秒。

但MouseDown事件似乎没有启动ColorAnimation,矩形的移动距离/持续时间也不正确。

private int i;
private Storyboard hitTargetStoryboard;
private List<double> disList;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    disList = new List<double>{.......};  // init with a list of values.

    /* Create a rectangle */
    Rectangle rect = new Rectangle();
    this.RegisterName("rect", rect);
    rect.Height = this.ActualHeight;
    rect.Width = 50;
    Canvas.SetTop(rect, 0);
    Canvas.SetLeft(rect, 0);

    /* Fill rect with a solid brush */
    SolidColorBrush targetRectBrush = new SolidColorBrush(Colors.Blue);
    this.RegisterName("targetRectBrush", targetRectBrush);
    rect.Fill = targetRectBrush;

    /* Add mouse down event */
    rect.MouseDown += Rect_MouseDown;

    /* Add rect to Canvas */
    myCanvas.Children.Add(rect);

    /* Create ColorAnimation to change color smoothly */
    ColorAnimation hitCA = new ColorAnimation();
    hitCA.To = Colors.Green;
    hitCA.Duration = TimeSpan.FromSeconds(0.3);
    hitCA.Completed += HitCA_Completed;

    /* Create storyboard and add ColorAnimation to it */
    hitTargetStoryboard = new Storyboard();
    Storyboard.SetTargetName(hitCA, "targetRectBrush");
    Storyboard.SetTargetProperty(hitCA, new PropertyPath(SolidColorBrush.ColorProperty));
    hitTargetStoryboard.Children.Add(hitCA);

    i = 0;
    TargetAnimation(i);
}


/* move the rect from 0--disList[i] */
private void TargetAnimation(int i)
{
    (this.FindName("rect") as Rectangle).Fill = Brushes.Blue;
    DoubleAnimation da = new DoubleAnimation();
    da.From = 0;
    da.To = disList[i];
    da.Duration = TimeSpan.FromSeconds(5);

    Storyboard.SetTargetName(da, "rect");
    Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
    Storyboard storyboard = new Storyboard();
    storyboard.Children.Add(da);
    storyboard.Completed += Storyboard_Completed;
    storyboard.Begin(this);
}

/* If rect clicked, it will change color to green */
private void Rect_MouseDown(object sender, MouseButtonEventArgs e)
{
    hitTargetStoryboard.Begin(this);
}

/* After color changed, rect starts over */
private void HitCA_Completed(object sender, EventArgs e)
{
    TargetAnimation(++i);
}

/* If rect not clicked, it will start over */
private void Storyboard_Completed(object sender, EventArgs e)
{
    TargetAnimation(++i);
}

更新

delete:(this.FindName(“rect”)as Rectangle).Fill = Brushes.Blue;

添加:hitCA.From = Colors.Blue;

ColorAnimation效果很好。

还是

如果我删除Storyboard_CompletedHitCA_Completed,则rect的移动进展顺利。如果我同时拥有这两种运动,那么运动就会出错。

更新2:

storyboard.Begin(this, true)方法中修改TargetAnimation(int i)

stroyboard.Stop(this)方法中添加:HitCA_Completed

未将isControallable设置为true,故事板将无法控制。

解决

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

(this.FindName("rect") as Rectangle).Fill = Brushes.Blue;

首先,将rect作为字段并直接设置Fill属性会更容易:

rect.Fill = Brushes.Blue;
但是,这对你的彩色动画没什么帮助。您已设置动画以使用targetRectBrush - 不再填充rect,因为您刚刚更换了它。删除那一行会为颜色设置动画。

<强>更新

这是一个稍微调整过的版本:

public partial class MainWindow
{
    private int i;
    private Storyboard hitTargetStoryboard;
    private List<double> disList;
    private Rectangle rect;

    public MainWindow()
    {
        InitializeComponent();
        Loaded += Window_Loaded;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        disList = new List<double> {10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };  // init with a list of values.

        /* Create a rectangle */
        rect = new Rectangle();
        this.RegisterName("rect", rect);
        rect.Height = this.ActualHeight;
        rect.Width = 50;
        Canvas.SetTop(rect, 0);
        Canvas.SetLeft(rect, 0);

        /* Fill rect with a solid brush */
        SolidColorBrush targetRectBrush = new SolidColorBrush(Colors.Blue);
        this.RegisterName("targetRectBrush", targetRectBrush);
        rect.Fill = targetRectBrush;

        /* Add mouse down event */
        rect.MouseDown += Rect_MouseDown;

        /* Add rect to Canvas */
        myCanvas.Children.Add(rect);

        /* Create ColorAnimation to change color smoothly */
        ColorAnimation hitCA = new ColorAnimation
            {
                From = Colors.Blue, // (Instead of setting Fill to Blue)
                To = Colors.Green,
                Duration = TimeSpan.FromSeconds(0.3),
                FillBehavior = FillBehavior.Stop, // Returns to Blue
            };
        hitCA.Completed += HitCA_Completed;

        /* Create storyboard and add ColorAnimation to it */
        hitTargetStoryboard = new Storyboard();
        Storyboard.SetTargetName(hitCA, "targetRectBrush");
        Storyboard.SetTargetProperty(hitCA, new PropertyPath(SolidColorBrush.ColorProperty));
        hitTargetStoryboard.Children.Add(hitCA);

        i = 0;
        TargetAnimation(i);
    }


    /* move the rect from 0--disList[i] */
    private void TargetAnimation(int i)
    {
        i = i % disList.Count; // Don't overflow

        DoubleAnimation da = new DoubleAnimation
            {
                From = 0,
                To = disList[i],
                Duration = TimeSpan.FromSeconds(5),
            };

        Storyboard.SetTargetName(da, "rect");
        Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
        Storyboard storyboard = new Storyboard();
        storyboard.Children.Add(da);
        storyboard.Completed += Storyboard_Completed;
        storyboard.Begin(this);
    }

    /* If rect clicked, it will change color to green */
    private void Rect_MouseDown(object sender, MouseButtonEventArgs e)
    {
        hitTargetStoryboard.Begin(this);
    }

    /* After color changed, rect starts over */
    private void HitCA_Completed(object sender, EventArgs e)
    {
        TargetAnimation(++i);
    }

    /* If rect not clicked, it will start over */
    private void Storyboard_Completed(object sender, EventArgs e)
    {
        TargetAnimation(++i);
    }
}

你看到距离/持续时间有什么问题?