Quantcast
Channel: Dailycode.Info - WPF
Viewing all articles
Browse latest Browse all 10

WPF - MVVM: Easy wait cursor implementation

$
0
0

Found a very good solution on StackOverflow.

Comes down to implementing a small class, better put it somewhere in your WPF framework or Shared library.

Call it like this:

UIHelper.SetBusyState();

The cursor will return to normal when the  application stops working.

 

Code:
using System;using System.Windows.Input;using System.Windows.Threading;namespace Global.Wpf
{publicstaticclassUIHelper
    {///<summary>///   A value indicating whether the UI is currently busy///</summary>privatestaticbool IsBusy;///<summary>/// Sets the busystate as busy.///</summary>publicstaticvoid 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>privatestaticvoid SetBusyState(bool busy)
        {if (busy != IsBusy)
            {
                IsBusy = busy;
                Mouse.OverrideCursor = busy ? Cursors.Wait : null;if (IsBusy)
                {newDispatcherTimer(TimeSpan.FromSeconds(0), DispatcherPriority.ApplicationIdle, dispatcherTimer_Tick, System.Windows.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>privatestaticvoid dispatcherTimer_Tick(object sender, EventArgs e)
        {var dispatcherTimer = sender asDispatcherTimer;if (dispatcherTimer != null)
            {
                SetBusyState(false);
                dispatcherTimer.Stop();
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images