C# WPF故事板和更新标签内容问题
我有一个故事板,可以帮助我在2个或更多画布对象集合之间执行动画。在其中一个画布中,我有一个标签,可以执行简单的倒计时更新-谁不工作 如果要更新此标签内容,在我在2个画布之间执行切换后,控制台会向我发出警告: System.Windows.Media.Animation警告:6:无法执行操作 因为指定的情节提要从未应用于此对象 交互控制。;行动`停止'; Storyboard='System.Windows.Media.Animation.Storyboard'; 故事板。HashCode='12309819'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='MyTest.MainWindow';HashCode='17070976'; TargetElement.Type='MyTest.MainWindow' 我的画布对象切换器的代码示例如下:C# WPF故事板和更新标签内容问题,c#,wpf,storyboard,wpf-controls,C#,Wpf,Storyboard,Wpf Controls,我有一个故事板,可以帮助我在2个或更多画布对象集合之间执行动画。在其中一个画布中,我有一个标签,可以执行简单的倒计时更新-谁不工作 如果要更新此标签内容,在我在2个画布之间执行切换后,控制台会向我发出警告: System.Windows.Media.Animation警告:6:无法执行操作 因为指定的情节提要从未应用于此对象 交互控制。;行动`停止'; Storyboard='System.Windows.Media.Animation.Storyboard'; 故事板。HashCode='12
public Storyboard _storyboard = new Storyboard();
const bool ToLeft = false;
const bool ToRight = true;
void ScreenSelector(FrameworkElement currentElement, FrameworkElement toNextElement, bool side)
{
if (_storyboard != null) _storyboard.Stop(this);
var ticknessLeft = new Thickness(Width, 0, -Width, 0);
var ticknessRight = new Thickness(-Width, 0, Width, 0);
var ticknessClient = new Thickness(0, 0, 0, 0);
var timeSpanStarting = TimeSpan.FromSeconds(0);
var timeSpanStopping = TimeSpan.FromSeconds(0.5);
var keyTimeStarting = KeyTime.FromTimeSpan(timeSpanStarting);
var keyTimeStopping = KeyTime.FromTimeSpan(timeSpanStopping);
toNextElement.Margin = side ? ticknessRight : ticknessLeft;
toNextElement.Visibility = Visibility.Visible;
var storyboardTemp = new Storyboard();
var currentThicknessAnimationUsingKeyFrames = new ThicknessAnimationUsingKeyFrames {BeginTime = timeSpanStarting};
Storyboard.SetTargetName(currentThicknessAnimationUsingKeyFrames, currentElement.Name);
Storyboard.SetTargetProperty(currentThicknessAnimationUsingKeyFrames, new PropertyPath("(FrameworkElement.Margin)"));
currentThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(ticknessClient, keyTimeStarting));
currentThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(side ? ticknessLeft : ticknessRight, keyTimeStopping));
storyboardTemp.Children.Add(currentThicknessAnimationUsingKeyFrames);
var nextThicknessAnimationUsingKeyFrames = new ThicknessAnimationUsingKeyFrames {BeginTime = timeSpanStarting};
Storyboard.SetTargetName(nextThicknessAnimationUsingKeyFrames, toNextElement.Name);
Storyboard.SetTargetProperty(nextThicknessAnimationUsingKeyFrames, new PropertyPath("(FrameworkElement.Margin)"));
nextThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(side ? ticknessRight : ticknessLeft, keyTimeStarting));
nextThicknessAnimationUsingKeyFrames.KeyFrames.Add(new SplineThicknessKeyFrame(ticknessClient, keyTimeStopping));
storyboardTemp.Children.Add(nextThicknessAnimationUsingKeyFrames);
storyboardTemp.Completed += (EventHandler)delegate
{
currentElement.Visibility = Visibility.Hidden;
_storyboard = null;
};
_storyboard = storyboardTemp;
BeginStoryboard(storyboardTemp);
}
现在我有一个MainWindow_KeyDown事件,检查按下的按钮:
switch (e.Key)
{
case Key.A:
{
ScreenSelector(fr_GameGrid,Layer_1, ToLeft);
GameEvents gm = new GameEvents();
gm.OnTimer();
}
break;
case Key.B:
{
ScreenSelector(Layer_1, fr_GameGrid, ToRight);
}
break;
我执行屏幕选择器并启动简单的倒计时。在GameEvent中,我有一个简单的代码:
public static void Countdown(int count, TimeSpan interval, Action<int> ts)
{
var dt = new DispatcherTimer { Interval = interval };
dt.Tick += (_, a) =>
{
if (count-- == 0)
dt.Stop();
else
ts(count);
};
ts(count);
dt.Start();
}
有人能告诉我哪里做错了吗?无法启动标签动画,这很奇怪-因为我在第6行更新了我的故事板->这个if\u故事板!=null _storyboard.Stopthis;到“如果”\u情节提要!=空的故事板。开始这个,真的;警告消失了 你的错误只是意味着你不能在一个从未应用过的对象上停止情节提要。我知道这一点——但是为什么我要在MainWindow类中插入我的倒计时,这样更新才能正常工作?
public void OnTimer()
{
/*
* VALIDATE TIME
*
*
* > OnAnswer
*/
Countdown(3, TimeSpan.FromSeconds(1), cur => lbTime.Content = cur.ToString(CultureInfo.InvariantCulture));
}