Add a BusyIndicator dynamically to a Silverlight ChildWindow

I used the following code to add a BusyIndicator dynamically to a ChildWindow in a Silverlight 4 application, I made it according to the following premises:
 
1. If it's a ChildWindow the Content property should be setted to a Grid.
2. If it's a Silverlight Page the Content property could be a ScrollViewer or a Grid.
 
After this, I created a static class name Utilities in which I wrote the following code:
 
  1. public static void SetBusyState(Object Content, bool IsBusy)  
  2.         {  
  3.             Grid pageGrid = null;  //a variable to handle the page/Window main Grid
  4.             BusyIndicator _indicator = null;  //the busyIndicator content (Silverlight 4 SDK)
  5.             try  
  6.             {  
  7.                 pageGrid = (Grid)Content;  //tries to parse the content into a Grid
  8.             }  
  9.             catch  //If an exception is thrown (is not a grid)
  10.             {  
  11.                 try  
  12.                 {  
  13.                     pageGrid = ((Grid)((ScrollViewer)Content).Content);  //we look if it's a ScrollViewer
  14.                 }  
  15.                 catch  //If an exception is Thrown (content is not a ScrollViewer)
  16.                 {  
  17.                     pageGrid = null;  //if not the control is set to null (you can try to parse another
  18.                                                                           //type)
  19.                 }  
  20.             }  
  21.             if (pageGrid != null)  //if the above code found the Grid
  22.             {  
  23.                 try  
  24.                 {  
  25.                     foreach (UIElement p in pageGrid.Children)  //Now we interate the Grid Children until
  26.                     {  
  27.                         try  
  28.                         {  
  29.                             _indicator = (BusyIndicator)p;  //the busyIndicator is found
  30.                             if (_indicator != null && IsBusy)//if we find it (and we want to set it to busy)
  31.                             {  
  32.                                  //set the busyIndicator to Busy (with a default message stored on the App resources).
  33.                                 _indicator.BusyContent = ApplicationStrings.PleaseWaitMessage;  
  34.                                 _indicator.IsBusy = IsBusy;  
  35.                             }  
  36.                             else if (_indicator != null && !IsBusy) //if we find it but is set to false 
  37.                             {  
  38.                                 pageGrid.Children.Remove(_indicator);  //just remove it from the Grid
  39.                             }  
  40.                         }  
  41.                         catch  
  42.                         {  
  43.                             continue;  //continues the cycle after every exception (control not found)
  44.                         }  
  45.                     }  
  46.                     if (_indicator == null && IsBusy)  //if the control is not added to the page/window
  47.                     {  
  48.                         _indicator = new BusyIndicator();  //creates a new BusyIndicator
  49.                         _indicator.Margin = new Thickness(0, 0, 0, 0);   //Set its position
  50.                         _indicator.Width = pageGrid.Width;  //Set its widht as the same as Grid
  51.                         _indicator.Height = pageGrid.Height;  //Set its height as the same as Grid
  52.                         _indicator.BusyContent = ApplicationStrings.PleaseWaitMessage;  //The Message
  53.                         _indicator.IsBusy = IsBusy;  //Set it to busy
  54.                         pageGrid.Children.Add(_indicator);  //Adds the control the Grid
  55.                     }  
  56.                 }  
  57.                 catch  
  58.                 {  
  59.                     Ocupado(Content, IsBusy);//this line here helps you when the Content state is not valid
  60.                     //generally happens when a IvalidOperationException is thron, so we call the method
  61.                     //until the state is valid
  62.                 }  
  63.             }  
  64.         }  
To use the SetBusyState function you just have to call it by passing the Page/Window Content property and the value you want to set to the BusyIndicator.
 
This is working for me right now, but I will also accept recommendations.
 
Regards,
José 
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now