【发布时间】:2020-11-30 09:53:38
【问题描述】:
我在为 .net WPF 中动态创建的 UserControl 运行 Storyboard 时遇到问题。
这些是我的课程示例:
class EventsPage {
// ...
public void AddEvent(Event @event) {
var eventUC = new EventUserContrl(@event);
eventUC.ExpandCollapseAnimation += ExpandCollapseAnimation;
EventsStackPanel.Children.Add(eventUC);
}
private ExpandCollapseAnimation(EventUserControl eventUC, double height, double time) {
// Create frames using custom functions for creating them.
var frames = new DoubleKeyFrameCollection() {
StoryBoardsBuilder.CreateEasingDoubleKeyFrame(0.0, eventUC.ActualHeight),
StoryBoardsBuilder.CreateEasingDoubleKeyFrame(time, destinationHeight)
};
// Create Animation.
var heightSizeAnimation= StoryBoardsBuilder.BuildDoubleAnimationUsingKeyFrames(
FillBehavior.Stop, frames, eventUC.Name, new PropertyPath("Height"));
// Create StoryBoard.
var storyboard = new Storyboard();
// Add Animations into StoryBoard.
storyboard.Children.Add(heightSizeAnimation);
// Create final function.
storyboard.Completed += (sender, e) => {
eventUC.Height = destinationHeight;
};
// Run animation.
storyboard.Begin(this);
}
// ...
}
启动后,在storyboard.Begin(this),显示异常:System.InvalidOperationException: „Name „” cannot be found in the namespace „ProjectName.Pages.EventsPage ”.”
我做了类似的事情,但在页面中手动放置用户控件,它可以工作,但这不会。
这是 StoryBuilder 代码:
public static EasingDoubleKeyFrame CreateEasingDoubleKeyFrame(
double frameTimeInSeconds,
double value) {
// Create double key frame.
return new EasingDoubleKeyFrame() {
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(frameTimeInSeconds)),
Value = value
};
}
public static DoubleAnimationUsingKeyFrames BuildDoubleAnimationUsingKeyFrames(
FillBehavior fillBehavior,
DoubleKeyFrameCollection keyFrames,
string targetName,
PropertyPath targetProperty) {
// Create animation.
var animation = new DoubleAnimationUsingKeyFrames();
// Set animation end behavior.
animation.FillBehavior = fillBehavior;
// Set animation frames.
animation.KeyFrames = keyFrames;
// Set animation target object.
Storyboard.SetTargetName(animation, targetName);
// Set animation target property.
Storyboard.SetTargetProperty(animation, targetProperty);
return animation;
}
【问题讨论】:
-
能否请您展示 StoryBoardsBuilder,尤其是 StoryBoardsBuilder.BuildDoubleAnimationUsingKeyFrames。看起来您正在尝试将情节提要目标设置为 eventUC.Name。我想应该是 eventUC 。请向建造者展示,以便我们真正看到您做错了什么。现在所有相关细节都被隐藏了。
-
@BionicCode 我用 StoryBuilder 的代码更新了帖子——这是用于快速实现故事板的工具代码。
-
谢谢。如何显示用户控件?
-
用户控件放置在 StackPanel
EventsStackPanel.Children.Add(eventUC);中,即 ScrollViewer 中
标签: c# .net wpf user-controls storyboard