ParameterizedThreadStart - PerformanceProgressBar - example of use - transition between two XAML pages
Hi, today I would like to describe (a little) my way to achieve an effect of page-initialization process using PerformanceProgressBar. I will put Visual Studio project file here later - very simple and marked in most important places to avoid going through unimportant parts of code. First of all I will explain my concept - I've got two pages and when I navigate from Page1 to Page2 - until Page2 gets loaded whole initialization process is being performed. During initialization process I can see some dots going from left to right (PerformanceProgressBar) and TextBlock object with "Loading...." as its Text's value.
When Page2 is being loaded I start new Thread object and do some initialization stuff in DoWork method (parameter of ParameterizedThreadStart delegate). I invoke this method with 2-second delay so PerformanceProgressBar can be displayed at least in one cycle. Use of Dispatcher is the most important because it helps to avoid invalid cross thread issue.
In my DoWork method I make my MainPivot container visible again and hide MainStackPanel which is a container for TextBlock ("Loading....") and PerformanceProgressBar - that's it. You have to add reference to the Microsoft.Phone.Controls.Toolkit and add xmlns : xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
- // HERE
- private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
- ParameterizedThreadStart pts = new ParameterizedThreadStart(DoWork);
- Thread t = new Thread(pts);
- // Set 2-second delay as the parameter of DoWork method
- // HERE
- public void DoWork(object milliseconds)
- // Wait..
- // and after 2 seconds:
- // DO SOMETHING HERE (..)
- // Hide MainStackPanel
- this.MainStackPanel.Visibility = System.Windows.Visibility.Collapsed;
- // Disable PerformanceProgressBar
- this.PerformanceProgressBar.IsEnabled = false;
- // Make MainPivot visible
- this.MainPivot.Visibility = System.Windows.Visibility.Visible;
- } // end of DoWork method