WP8控件进度条(ProgressBar)的使用

--前台代码 

<Grid x:Name="ContentPanel"  Grid.Row="1" Margin="0,0,24,0">
            <ProgressBar x:Name="ProgressBar1" HorizontalAlignment="Left" Height="71" Margin="42,244,0,0" VerticalAlignment="Top" Width="308" Value="50"/>
            <TextBlock HorizontalAlignment="Left" Height="38" Margin="42,10,0,0" TextWrapping="Wrap" Text="请选择类型" VerticalAlignment="Top" Width="330"/>
            <RadioButton x:Name="rad1" Content="Determinate类型" HorizontalAlignment="Left" Margin="42,53,0,0" VerticalAlignment="Top" Width="330" IsChecked="True"/>
            <RadioButton x:Name="rad2" Content="Indeterminate类型" HorizontalAlignment="Left" Margin="42,146,0,0" VerticalAlignment="Top" Width="330"/>
            <Button x:Name="begin" Content="启动" HorizontalAlignment="Left" Margin="42,320,0,0" VerticalAlignment="Top" Width="295"/>
            <Button x:Name="canel" Content="取消" HorizontalAlignment="Left" Margin="42,392,0,0" VerticalAlignment="Top" Width="295"/>
        </Grid>

--后台代码

using Microsoft.Phone.Controls;

using Microsoft.Phone.Shell;

using PhoneFirst.Resources;

using System.ComponentModel;



namespace PhoneFirst

{

    public partial class MainPage : PhoneApplicationPage

    {

        private BackgroundWorker backgroundworker;

        // 构造函数

        public MainPage()

        {

            

            InitializeComponent();

            ProgressBar1.Visibility = Visibility.Collapsed;

            begin.Click += begin_Click;

            canel.Click += canel_Click;



          

        }



        void canel_Click(object sender, RoutedEventArgs e)

        {

            ProgressBar1.Visibility = Visibility.Collapsed;

        }



        void begin_Click(object sender, RoutedEventArgs e)

        {

            ProgressBar1.Visibility = Visibility.Visible;

            if (rad1.IsChecked == true)

            {

                ProgressBar1.IsIndeterminate = false;

                backgroundworker = new BackgroundWorker();//创建一个后台处理类的对象

                //调用RunWorkerAsync后台操作时引发此事件,即后台要处理的事情写在这个事件里面

                backgroundworker.DoWork += backgroundworker_DoWork;

                //当后台操作完成事件

                backgroundworker.RunWorkerCompleted += backgroundworker_RunWorkerCompleted;

                //当处理进度(ReportProgress)被激活后,进度的改变将触发ProgressChanged事件

                backgroundworker.ProgressChanged += backgroundworker_ProgressChanged;

                //设置为可报告进度情况的后台处理

                backgroundworker.WorkerReportsProgress = true;

                backgroundworker.RunWorkerAsync();

            }

            else

            {

                ProgressBar1.Value = 0;

                ProgressBar1.IsIndeterminate = true;

            }

        }

        //进度改变处理

        void backgroundworker_ProgressChanged(object sender, ProgressChangedEventArgs e)

        {

            Dispatcher.BeginInvoke(() => {

                //把进度改变的值赋给ProgressBar1进度条的值

                ProgressBar1.Value = e.ProgressPercentage;

            });

        }

        //后台操作完成

        void backgroundworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

        {

            Dispatcher.BeginInvoke(() => { 

            //隐藏进度条;

                ProgressBar1.Visibility = Visibility.Collapsed;

            });

        }

        //后台操作处理

        void backgroundworker_DoWork(object sender, DoWorkEventArgs e)

        {

            for (int i = 0; i < 100; i++)

            {

                i += 10;

                //赋值当前进度的值,

                backgroundworker.ReportProgress(i);

                //为了能看到进度条的效果。这里用进程暂停了1秒

                System.Threading.Thread.Sleep(1000);

            }

        }



        

        public ResourceDictionary Resources { get; set; }



       

        

    }

    

}

 

你可能感兴趣的:(ProgressBar)