在 WPF 中对比例布局转换进行动画处理时出现异常
我已经用 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"
源=“WindowsBase”ParamName=“dp” 堆栈跟踪: 在 System.Windows.DependencyObject.GetValue(DependencyProperty DP) 在 System.Windows.Media.Animation.Storyboard.ProcessComplexPath(HybridDictionary 时钟映射、依赖对象 targetObject、PropertyPath 路径、 动画时钟动画时钟, HandoffBehavior 切换行为,Int64 层) 在 System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟 当前时钟、依赖对象 包含对象、INameScope 名称范围、依赖对象 父对象,字符串父对象名称, PropertyPath父PropertyPath, HandoffBehavior 切换行为, 混合字典时钟映射,Int64 层) 在 System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟 当前时钟、依赖对象 包含对象、INameScope 名称范围、依赖对象 父对象,字符串父对象名称, PropertyPath父PropertyPath, HandoffBehavior 切换行为, 混合字典时钟映射,Int64 层) 在 System.Windows.Media.Animation.Storyboard.BeginCommon(DependencyObject 包含对象、INameScope 名称范围、切换行为 handoffBehavior,布尔值 isControllable,Int64层) 在 System.Windows.Media.Animation.Storyboard.Begin() 在 StormFront.NexusDesigner.ScaleCanvasAnimation(双 规模)在 C:\Documents 和 设置\lbeaver\Desktop\StormFront\WPF\StormFront\StormFront\NexusDesigner.xaml.cs:行 第544章
如何阻止此异常发生?
I have create a storyboard in C# to animation a scale transform on a canvas. The scale transform is a layout transform. Here is my C# code for the animation:
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)"));
}
When i run this animation it throws the following exception.
System.ArgumentNullException was
caught Message="Value cannot be
null.\r\nParameter name: dp"
Source="WindowsBase" ParamName="dp"
StackTrace:
at System.Windows.DependencyObject.GetValue(DependencyProperty
dp)
at System.Windows.Media.Animation.Storyboard.ProcessComplexPath(HybridDictionary
clockMappings, DependencyObject
targetObject, PropertyPath path,
AnimationClock animationClock,
HandoffBehavior handoffBehavior, Int64
layer)
at System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(Clock
currentClock, DependencyObject
containingObject, INameScope
nameScope, DependencyObject
parentObject, String parentObjectName,
PropertyPath parentPropertyPath,
HandoffBehavior handoffBehavior,
HybridDictionary clockMappings, Int64
layer)
at System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(Clock
currentClock, DependencyObject
containingObject, INameScope
nameScope, DependencyObject
parentObject, String parentObjectName,
PropertyPath parentPropertyPath,
HandoffBehavior handoffBehavior,
HybridDictionary clockMappings, Int64
layer)
at System.Windows.Media.Animation.Storyboard.BeginCommon(DependencyObject
containingObject, INameScope
nameScope, HandoffBehavior
handoffBehavior, Boolean
isControllable, Int64 layer)
at System.Windows.Media.Animation.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:
How do i stop this exception from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了我的问题。问题出在属性路径上。我正在使用 UIElement 并且应该使用 FrameworkElement。
所以这一行:
应该是:
I found my problem. The problem is in the property path. I was using UIElement and should have been using FrameworkElement.
So this line:
should be: