Point3DCollectionAnimation中的collection.Clear问题

来源:互联网 发布:中文域名管理办法 编辑:程序博客网 时间:2024/06/13 01:53

模仿Charles Petzold的PointCollectionAnimation写了一个Point3DCollectionAnimation,自己按照理解的写了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using Petzold.AnimationExtensions;

    
class Point3DCollectionAnimation:Point3DCollectionAnimationBase
    {

        Point3DCollection p3dcDst 
= new Point3DCollection();

        
public static DependencyProperty FromProperty = DependencyProperty.Register("From"typeof(Point3DCollection), typeof(Point3DCollectionAnimation));
        
public static DependencyProperty ToProperty = DependencyProperty.Register("To"typeof(Point3DCollection), typeof(Point3DCollectionAnimation));

        
public Point3DCollection From
        {
            
set { SetValue(FromProperty, value); }
            
get { return (Point3DCollection)GetValue(FromProperty); }
        }

        
public Point3DCollection To
        {
            
set { SetValue(ToProperty, value); }
            
get { return (Point3DCollection)GetValue(ToProperty); }
        }

        
protected override Point3DCollection GetCurrentValueCore(Point3DCollection defaultOriginValue, Point3DCollection defaultDestinationValue, AnimationClock animationClock)
        {
            
if (animationClock.CurrentProgress == null)
                
return null;
            
double progress = animationClock.CurrentProgress.Value;

            Point3DCollection p3dcFrom 
= From ?? defaultOriginValue;
            Point3DCollection p3dcTo 
= To ?? defaultDestinationValue;

            p3dcDst.Clear();
            
            
int count = defaultOriginValue.Count;
            
            
for (int i = 0; i < count; i++)
            {
                p3dcDst.Add(
new Point3D((1 - progress) * p3dcFrom[i].X + progress * p3dcTo[i].X,
                                          (
1 - progress) * p3dcFrom[i].Y + progress * p3dcTo[i].Y,
                                          (
1 - progress) * p3dcFrom[i].Z + progress * p3dcTo[i].Z));
            }
            
            
return p3dcDst;
        }

        
protected override System.Windows.Freezable CreateInstanceCore()
        {
            
return new Point3DCollectionAnimation();
        }
    }

但实际的效果却总出不来,我设想的曲面动画总是不出现,弄了很久,把这些语句一个个调试过之后发现,似乎是p3dcDst.Clear出的问题,Clear之后再Add一批新点后,这些值在debug时看着是好的,但返回给WPF绘图时它就是没反应,而如果用p3dcDst = new Point3DCollection()替代效果才能出来。翻查原来Charles Petzold写的PointCollectionAnimation,才发现他用了一个flip的变量,同时定义了两个PointCollection实例,在操作时不停地flip。原来人家是知道这个问题的,奇怪怎么没有看到这方面的资料。。

于是给我的类加入了同样的机制:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using Petzold.AnimationExtensions;

    
class Point3DCollectionAnimation:Point3DCollectionAnimationBase
    {

        Point3DCollection p3dcDst1 
= new Point3DCollection();
        Point3DCollection p3dcDst2 
= new Point3DCollection();

        
bool flip = true;

        
public static DependencyProperty FromProperty = DependencyProperty.Register("From"typeof(Point3DCollection), typeof(Point3DCollectionAnimation));
        
public static DependencyProperty ToProperty = DependencyProperty.Register("To"typeof(Point3DCollection), typeof(Point3DCollectionAnimation));

        
public Point3DCollection From
        {
            
set { SetValue(FromProperty, value); }
            
get { return (Point3DCollection)GetValue(FromProperty); }
        }

        
public Point3DCollection To
        {
            
set { SetValue(ToProperty, value); }
            
get { return (Point3DCollection)GetValue(ToProperty); }
        }

        
protected override Point3DCollection GetCurrentValueCore(Point3DCollection defaultOriginValue, Point3DCollection defaultDestinationValue, AnimationClock animationClock)
        {
            
if (animationClock.CurrentProgress == null)
                
return null;
            
double progress = animationClock.CurrentProgress.Value;

            Point3DCollection p3dcFrom 
= From ?? defaultOriginValue;
            Point3DCollection p3dcTo 
= To ?? defaultDestinationValue;

            Point3DCollection p3dcDst
=flip ? p3dcDst1 : p3dcDst2 ;
            flip 
= !flip;
            p3dcDst.Clear();
            
            
int count = defaultOriginValue.Count;
            
            
for (int i = 0; i < count; i++)
            {
                p3dcDst.Add(
new Point3D((1 - progress) * p3dcFrom[i].X + progress * p3dcTo[i].X,
                                          (
1 - progress) * p3dcFrom[i].Y + progress * p3dcTo[i].Y,
                                          (
1 - progress) * p3dcFrom[i].Z + progress * p3dcTo[i].Z));
            }
            
            
return p3dcDst;
        }

        
protected override System.Windows.Freezable CreateInstanceCore()
        {
            
return new Point3DCollectionAnimation();
        }
}

效果就出来了。

原创粉丝点击