Silverlight - 在繁忙的UI线程上显示BusyIndi​​cator

时间:2012-04-19 07:38:30

标签: silverlight silverlight-toolkit

当UI中发生繁重任务时,可以显示忙碌指示器吗?在我们的应用程序中,大多数长时间运行的任务都是渲染控件,我们需要在此控件渲染时显示指示符。

3 个答案:

答案 0 :(得分:0)

在UI线程忙时,您无法显示加载动画,但您可以使用适当的文本显示静态通知,例如TextBlock控件。但是,如果在更新TextBlock控件的文本后立即在UI线程上启动长时间运行操作,则控件的UI将不会更新,直到操作结束。要解决此问题,您可以使用以下问题的答案中描述的技术:Showing a text indicator before freezing the Silverlight UI thread

答案 1 :(得分:0)

首先,考虑使用后台线程来执行长时间运行的工作。如果这是不可能的,即它真正花费很多时间在UI线程上加载UI组件,那么你当然可以在加载部分的顶部放置一个忙碌指示符作为叠加层,然后在加载所有内容时隐藏叠加层。

顺便说一句,很难编写响应式多线程应用程序,而无需将UI逻辑与UI分离。研究'MVVM'模式。使用MVVM将使您的应用程序实现跨越式发展。

我会说没有认真的应用程序使用'代码隐藏',即一切都是通过DataContext,数据绑定,ViewModels和命令完成的。

否则,请查看使用Tasks或BackgroundWorker并了解Dispatcher。

答案 2 :(得分:0)

尝试使用DispatcherTimer在忙碌任务被调用之前设置忙指示符(通过使用延迟)。

然后,您可以在繁重任务完成后禁用指示器。

适合我。

'enable busy indicator & set up the timer'
 Private Sub renderControl(ByVal sender As Object, ByVal e As RoutedEventArgs)

        _busyIndicator.IsBusy = True

        Dim timer As New DispatcherTimer
        timer.Interval = TimeSpan.FromMilliseconds(100)
        AddHandler timer.Tick, AddressOf renderControl_TimerTick
        timer.Start()

    End Sub

'do your heavy task, disable busy indicator then stop the timer'
  Private Sub renderControl_TimerTick(ByVal sender As Object, ByVal e As EventArgs)

        DoStuff()

        _busyIndicator.IsBusy = False

        'Stop the timer'
        TryCast(sender, DispatcherTimer).[Stop]() 

    End Sub

希望这有帮助!