【问题标题】:Exception animating a scale layout transform in WPF在 WPF 中为缩放布局变换设置动画的异常
【发布时间】:2011-01-14 13:04:10
【问题描述】:

我在 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 是 抓到消息=“值不能是 空。\r\n参数名称:dp"
源="WindowsBase" 参数名称="dp" 堆栈跟踪: 在 System.Windows.DependencyObject.GetValue(DependencyProperty dp) 在 System.Windows.Media.Animation.Storyboard.ProcessComplexPath(HybridDictionary 时钟映射,依赖对象 targetObject,PropertyPath 路径, AnimationClock 动画时钟, HandoffBehavior 切换行为,Int64 层) 在 System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟 当前时钟,依赖对象 包含对象,INameScope 名称范围,依赖对象 父对象,字符串父对象名称, 属性路径父属性路径, HandoffBehavior 切换行为, HybridDictionary 时钟映射,Int64 层) 在 System.Windows.Media.Animation.Storyboard.ClockTreeWalkRecursive(时钟 当前时钟,依赖对象 包含对象,INameScope 名称范围,依赖对象 父对象,字符串父对象名称, 属性路径父属性路径, HandoffBehavior 切换行为, HybridDictionary 时钟映射,Int64 层) 在 System.Windows.Media.Animation.Storyboard.BeginCommon(DependencyObject 包含对象,INameScope 名称范围,切换行为 切换行为,布尔值 isControllable,Int64 层) 在 System.Windows.Media.Animation.Storyboard.Begin() 在 StormFront.NexusDesigner.ScaleCanvasAnimation(双 比例)在 C:\Documents 和 设置\lbeaver\Desktop\StormFront\WPF\StormFront\StormFront\NexusDesigner.xaml.cs:line 544 内部异常:

如何阻止此异常的发生?

【问题讨论】:

  • 您能否更好地格式化您的文本?堆栈跟踪不可读
  • NexusDesigner.xaml.cs:line 544 第 554 行?现在杀了我。
  • 堆栈跟踪没有任何帮助。如您所见,异常的来源是 WindowsBase 而不是我的代码。
  • 第 554 行也是调用 storyboard.begin 的时间

标签: c# wpf animation layouttransform


【解决方案1】:

我发现了我的问题。问题出在属性路径中。我使用的是 UIElement,应该一直在使用 FrameworkElement。

所以这一行:

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

应该是:

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

【讨论】:

    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多