我在 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 被捕获 Message="Value 不能为 null。\r\nParameter name: dp"
Source="WindowsBase" ParamName="dp" StackTrace:在 System.Windows.DependencyObject.GetValue(DependencyProperty dp) 在 System.Windows.Media.Animation.Storyboard.ProcessComplexPath(HybridDictionary clockMappings, DependencyObject targetObject, PropertyPath path, AnimationClock animationClock, HandoffBehavior handoffBehavior,Int64 层)在 System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟 currentClock,DependencyObject containsObject,INameScope nameScope,DependencyObject parentObject,String parentObjectName,PropertyPath parentPropertyPath,HandoffBehavior handoffBehavior,HybridDictionary clockMappings,Int64 层)在 System.Windows。 Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟 currentClock,System.Windows.Media.Animation.Storyboard.BeginCommon 中的 DependencyObject containsObject、INameScope nameScope、DependencyObject parentObject、String parentObjectName、PropertyPath parentPropertyPath、HandoffBehavior handoffBehavior、HybridDictionary clockMappings、Int64 层)(DependencyObject containsObject、INameScope nameScope、HandoffBehavior handoffBehavior、Boolean isControllable、Int64层)在 System.Windows.Media.Animation.Storyboard.Begin() 在 C:\Documents and Settings\lbeaver\Desktop\StormFront\WPF\StormFront\StormFront\NexusDesigner.xaml 中的 StormFront.NexusDesigner.ScaleCanvasAnimation(双比例)。 cs:第 544 行内部异常:HandoffBehavior handoffBehavior, HybridDictionary clockMappings, Int64 layer) at System.Windows.Media.Animation.Storyboard.BeginCommon(DependencyObject containsObject, INameScope nameScope, HandoffBehavior handoffBehavior, Boolean isControllable, Int64 layer) at System.Windows.Media.Animation.Storyboard.Begin( ) 在 C:\Documents and Settings\lbeaver\Desktop\StormFront\WPF\StormFront\StormFront\NexusDesigner.xaml.cs:line 544 InnerException 中的 StormFront.NexusDesigner.ScaleCanvasAnimation(Double scale):HandoffBehavior handoffBehavior, HybridDictionary clockMappings, Int64 layer) at System.Windows.Media.Animation.Storyboard.BeginCommon(DependencyObject containsObject, INameScope nameScope, HandoffBehavior handoffBehavior, Boolean isControllable, Int64 layer) at System.Windows.Media.Animation.Storyboard.Begin( ) 在 C:\Documents and Settings\lbeaver\Desktop\StormFront\WPF\StormFront\StormFront\NexusDesigner.xaml.cs:line 544 InnerException 中的 StormFront.NexusDesigner.ScaleCanvasAnimation(Double scale):Storyboard.Begin() at StormFront.NexusDesigner.ScaleCanvasAnimation(Double scale) in C:\Documents and Settings\lbeaver\Desktop\StormFront\WPF\StormFront\StormFront\NexusDesigner.xaml.cs:line 544 InnerException:Storyboard.Begin() at StormFront.NexusDesigner.ScaleCanvasAnimation(Double scale) in C:\Documents and Settings\lbeaver\Desktop\StormFront\WPF\StormFront\StormFront\NexusDesigner.xaml.cs:line 544 InnerException:
我如何阻止这种异常的发生?