在 WPF 中对比例布局转换进行动画处理时出现异常

发布于 2024-08-20 21:06:33 字数 2422 浏览 3 评论 0原文

我已经用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-08-27 21:06:33

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

所以这一行:

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

应该是:

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

I found my problem. The problem is in the property path. I was using UIElement and should have been using FrameworkElement.

So this line:

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

should be:

Storyboard.SetTargetProperty(myDoubleAnimation1, (PropertyPath)new PropertyPathConverter().ConvertFromString("(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleX)"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文