Point3DCollectionAnimation中的collection.Clear问题

模仿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();
        }
}

效果就出来了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值