WPF应用程序忙于数据绑定时如何显示一个waitcursor [英] How to show a waitcursor when the WPF application is busy databinding

查看:218
本文介绍了WPF应用程序忙于数据绑定时如何显示一个waitcursor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MVVM模式的WPF应用程序,有时在忙于做用户必须等待的事情时,必须显示一个waitcursor.感谢此页面上的答案组合:在应用程序忙时显示沙漏 ,我有一个几乎可以解决的解决方案(尽管本质上并不是真正的MVVM). 每当我在视图模型中做一些耗时的事情时,我都会这样做:

I have a WPF application using the MVVM pattern that sometimes have to show a waitcursor when it is busy doing something the user has to wait for. Thanks to a combination of answers on this page: display Hourglass when application is busy, I have a solution that almost works (although it is not really MVVM in spirit). Whenever I do something time-consuming in my viewmodels I do this:

using (UiServices.ShowWaitCursor())
{
.. do time-consuming logic
this.SomeData = somedata;
}

(ShowWaitCursor()返回一个IDisposable,它显示等待光标,直到被丢弃为止) 我的示例的最后一行是我设置一些属性的地方.此属性绑定在我的XAML中,例如像这样:

(ShowWaitCursor() returns a IDisposable that shows the waitcursor until it is being disposed of) The last line in my example is where I set some property. This property is bound in my XAML, e.g. like this:

<ItemsControl ItemsSource="{Binding SomeData}" /> 

但是,由于这可能是一长串对象,有时带有复杂的数据模板等,因此实际的绑定和呈现有时会花费大量时间.由于此绑定发生在我的using语句之外,因此waitcursor将在用户实际等待结束之前消失.

However, since this could be a long list of objects and sometimes with complex datatemplates, etc. the actual binding and rendering sometime takes a considerable amount of time. Since this binding takes places outside of my using statement the waitcursor will go away before the actual wait is over for the user.

所以我的问题是如何在考虑数据绑定的WPF MVVM应用程序中做一个等待光标?

So my question is how to do a waitcursor in a WPF MVVM application that takes databinding into account?

推荐答案

Isak的答案对我不起作用,因为它没有解决在实际等待用户时如何采取行动的问题. 我最终这样做:每当我开始做耗时的事情时,我都会调用一个辅助方法.此帮助程序方法更改光标,然后创建一个DispatcherTimer,在应用程序空闲时将调用它.调用它时,会将鼠标光标移回:

Isak's answer did not work for me, because it did not solve the problem of how to act when the actual wait is over for the user. I ended up doing this: Everytime I start doing something timeconsuming, I call a helper-method. This helper method changes the cursor and then creates a DispatcherTimer that will be called when the application is idle. When it is called it sets the mousecursor back:

/// <summary>
///   Contains helper methods for UI, so far just one for showing a waitcursor
/// </summary>
public static class UiServices
{

     /// <summary>
     ///   A value indicating whether the UI is currently busy
     /// </summary>
     private static bool IsBusy;

     /// <summary>
     /// Sets the busystate as busy.
     /// </summary>
     public static void SetBusyState()
     {
          SetBusyState(true);
     }

     /// <summary>
     /// Sets the busystate to busy or not busy.
     /// </summary>
     /// <param name="busy">if set to <c>true</c> the application is now busy.</param>
     private static void SetBusyState(bool busy)
     {
          if (busy != IsBusy)
          {
               IsBusy = busy;
               Mouse.OverrideCursor = busy ? Cursors.Wait : null;

               if (IsBusy)
               {
                   new DispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, dispatcherTimer_Tick, Application.Current.Dispatcher);
               }
          }
     }

     /// <summary>
     /// Handles the Tick event of the dispatcherTimer control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     private static void dispatcherTimer_Tick(object sender, EventArgs e)
     {
          var dispatcherTimer = sender as DispatcherTimer;
          if (dispatcherTimer != null)
          {
              SetBusyState(false);
              dispatcherTimer.Stop();
          }
     }
}

这篇关于WPF应用程序忙于数据绑定时如何显示一个waitcursor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆