使用计时器更改WPF中的可见性
我有一个应用程序,需要使用计时器切换某些控件的可见性。每5秒钟,一些控件消失,而另一些控件出现。当我使用计时器时,它表示无法更改可见性,因为计时器线程不是控件的所有者 我们怎么能绕过它 Tks您可以使用SynchronizationContext.Post或Dispatcher.Invoke将UIElement.Visible属性集重新整理到UI线程上 这可以很简单,如下所示:使用计时器更改WPF中的可见性,wpf,multithreading,timer,Wpf,Multithreading,Timer,我有一个应用程序,需要使用计时器切换某些控件的可见性。每5秒钟,一些控件消失,而另一些控件出现。当我使用计时器时,它表示无法更改可见性,因为计时器线程不是控件的所有者 我们怎么能绕过它 Tks您可以使用SynchronizationContext.Post或Dispatcher.Invoke将UIElement.Visible属性集重新整理到UI线程上 这可以很简单,如下所示: App.SynchronizationContext.Post(new SendOrPostCallback((sta
App.SynchronizationContext.Post(new SendOrPostCallback((state) =>
{
theControl.Visible = Visibilty.Visible;
}), null);
要么使用它将其返回到UI线程,要么您可能想考虑使用动画来代替?在Blend中设置时间线非常简单,可以完全在XAML中完成这项工作。
只需使用。每次勾选时调用的代码都会自动在UI线程上运行 例如,来自MSDN// DispatcherTimer setup
var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,5);
dispatcherTimer.Start();
// System.Windows.Threading.DispatcherTimer.Tick handler
//
// Updates the current seconds display
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// Updating the Label which displays the current second
lblSeconds.Content = DateTime.Now.Second;
}
这一点,但他想做的事情可以通过UI中的动画轻松完成。Tks,它成功了。仅供参考,我没有考虑使用动画,但这可能是以后的一个好主意。