在WPF中为缩放布局转换设置动画的异常

时间:2010-02-08 20:35:07

标签: c# wpf animation layouttransform

我在C#中创建了一个故事板,用于在画布上动画缩放变换。比例变换是布局变换。这是动画的C#代码:

Storyboard Configuring = new Storyboard();
if (NexusRoot != null)
{
var current = (NexusRoot.LayoutTransform as ScaleTransform).ScaleX;

Duration duration = new Duration(TimeSpan.FromSeconds(1));

DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
myDoubleAnimation1.Duration = duration;
Configuring.Children.Add(myDoubleAnimation1);
myDoubleAnimation1.From = current;
myDoubleAnimation1.To = scale;
Storyboard.SetTarget(myDoubleAnimation1, NexusRoot);
Storyboard.SetTargetProperty(myDoubleAnimation1, (PropertyPath)new PropertyPathConverter().ConvertFromString("(UIElement.LayoutTransform).(ScaleTransform.ScaleX)"));

DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();
myDoubleAnimation2.Duration = duration;
Configuring.Children.Add(myDoubleAnimation2);
myDoubleAnimation2.From = current;
myDoubleAnimation2.To = scale;

Storyboard.SetTargetName(myDoubleAnimation2, "NexusRoot");
Storyboard.SetTargetProperty(myDoubleAnimation2, (PropertyPath)new PropertyPathConverter().ConvertFromString("(UIElement.LayoutTransform).(ScaleTransform.ScaleY)"));
}

当我运行此动画时,它会抛出以下异常。

  

System.ArgumentNullException是   抓住消息=“价值不可能   null。\ r \ n参数名称:dp“
  Source =“WindowsBase”ParamName =“dp”   堆栈跟踪:          在System.Windows.DependencyObject.GetValue(DependencyProperty   DP)          在System.Windows.Media.Animation.Storyboard.ProcessComplexPath(HybridDictionary   clockMappings,DependencyObject   targetObject,PropertyPath路径,   AnimationClock animationClock,   HandoffBehavior handoff行为,Int64   层)          在System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟   currentClock,DependencyObject   containsObject,INameScope   nameScope,DependencyObject   parentObject,String parentObjectName,   PropertyPath parentPropertyPath,   HandoffBehavior handoff行为,   HybridDictionary clockMappings,Int64   层)          在System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟   currentClock,DependencyObject   containsObject,INameScope   nameScope,DependencyObject   parentObject,String parentObjectName,   PropertyPath parentPropertyPath,   HandoffBehavior handoff行为,   HybridDictionary clockMappings,Int64   层)          在System.Windows.Media.Animation.Storyboard.BeginCommon(DependencyObject   containsObject,INameScope   nameScope,HandoffBehavior   handoffBehavior,布尔   isControllable,Int64层)          在System.Windows.Media.Animation.Storyboard.Begin()          在StormFront.NexusDesigner.ScaleCanvasAnimation(Double   C:\ Documents和   设置\ lbeaver \桌面\ StormFront \ WPF \ StormFront \ StormFront \ NexusDesigner.xaml.cs:行   544 InnerException:

如何阻止此异常发生?

1 个答案:

答案 0 :(得分:3)

我发现了我的问题。问题出在属性路径上。我正在使用UIElement,应该使用FrameworkElement。

所以这一行:

Storyboard.SetTargetProperty(myDoubleAnimation1, (PropertyPath)new PropertyPathConverter().ConvertFromString("(UIElement.LayoutTransform).(ScaleTransform.ScaleX)"));

应该是:

Storyboard.SetTargetProperty(myDoubleAnimation1, (PropertyPath)new PropertyPathConverter().ConvertFromString("(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleX)"));