ThicknessAnimation InvalidOperationException

时间:2016-04-10 19:22:08

标签: c# wpf

我在窗口方法中有这个代码(AddServer是用户控件)

addServer = new AddServer();
addServer.Height = 300;
addServer.Width = 570;

this.RegisterName("addServerPanel", addServer);
Main.Children.Add(addServer);

// 252, 248, 26, 0
addServer.Margin = new Thickness(252, 550, 26, 0);


ThicknessAnimation thicknessAnimation = new ThicknessAnimation();
thicknessAnimation.From = new Thickness(252, 550, 26, 0);
thicknessAnimation.To = new Thickness(252, 248, 26, 0);
thicknessAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));

Storyboard.SetTargetName(thicknessAnimation, "addServerPanel");
Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath(MarginProperty));

Storyboard storyboard = new Storyboard();
storyboard.Children.Add(thicknessAnimation);

storyboard.Begin();

当故事板开始时,我得到一个InvalidOperationException。这是说他无法解析名称addServerPanel所以我认为窗口没有注册usercontrol的名称。我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以使用SetTarget方法直接将其设置为addServer对象

,而不是使用目标名称
Storyboard.SetTarget(thicknessAnimation, addServer);