Dailycode.info

Short solution for short problems

WPF - MVVM: Easy wait cursor implementation

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
{
    public static class UIHelper
    {
        /// <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, 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>
        private static void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            var dispatcherTimer = sender as DispatcherTimer;
            if (dispatcherTimer != null)
            {
                SetBusyState(false);
                dispatcherTimer.Stop();
            }
        }
    }
}

iOS: Performing a segue using a splash screen.

My application was using a dash board, from where the user navigates through the application. Works file until some large forms take more then a second or so to load. Then it might appear that the form is doing nothing. So a simple solution is to show a waiting screen while performing the segue. The only little trick is that you call the perform segue with a delay and before you do that show your waiting screen. Else the thread will wait to output to GUI until the segue is done and you will not see the splash screen.

So here just a little piece of code:

-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath

{

    @try

    {

        NSString * label = [store.HomeButtonsArray objectAtIndex:indexPath.item];

        //Show the waiting popup (Change it here to match your waiting popup)

        hud = [GeneralFunctionsGetMBProgressHUDForView:self.view];

        hud.detailsLabelText = [[storeTranslate: @"$MOB$Loading"] stringByAppendingString:[@" : "stringByAppendingString:label]];

        AudioServicesPlaySystemSound(0x450);

        

        [self performSelector:@selector(segueWithLabel:) withObject:label afterDelay:0.2];

        //[self performSegueWithIdentifier:[NSString stringWithFormat:@"DashB%@",label] sender:nil];

    }

    @catch (NSException *exception) {

        

    }

    @finally {

        

    }

}

 

-(void) segueWithLabel:(id) label

{

    [selfperformSegueWithIdentifier:[NSStringstringWithFormat:@"DashB%@",label] sender:nil];

}

 
if a collection view item is pressed, the segue starts after 0.2 seconds, but first a waiting popup will be send to the screen.
Then when the view disappears, I hide the waiting popup:
 

-(void) viewDidDisappear:(BOOL)animated

{

    [MBProgressHUDhideAllHUDsForView:self.viewanimated:YES];

}