0
我想创建一个计时器,返回到我的WPF应用程序的主菜单,比方说,30秒的不活动。但我得到错误“调用线程无法访问此对象,因为不同的线程拥有它。”它发生在FadeOut()
在storyboard.Begin(uc);
调用线程无法访问此对象
我见过一些涉及调用调度员的解决方案,但我不知道如何申请我的情况?
public void ResetScreen()
{
if (!mainScreen)
{
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 1000;
myTimer.Start();
}
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
TransitionContent(oldScreen, newScreen);
}
private void FadeIn(FrameworkElement uc)
{
DoubleAnimation dAnimation = new DoubleAnimation();
dAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.0));
dAnimation.From = 0;
dAnimation.To = 1;
Storyboard.SetTarget(dAnimation, uc);
Storyboard.SetTargetProperty(dAnimation, new PropertyPath(OpacityProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(dAnimation);
storyboard.Begin(uc);
}
private void FadeOut(FrameworkElement uc)
{
DoubleAnimation dAnimation = new DoubleAnimation();
dAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.0));
dAnimation.From = 1;
dAnimation.To = 0;
Storyboard.SetTarget(dAnimation, uc);
Storyboard.SetTargetProperty(dAnimation, new PropertyPath(OpacityProperty));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(dAnimation);
storyboard.Begin(uc);
}
private void TransitionContent(FrameworkElement oldScreen, FrameworkElement newScreen)
{
FadeOut(oldScreen);
FadeIn(newScreen);
}
此解决方案的工作原理,谢谢。 – Michael 2012-03-06 22:35:10