进度条ProgressBar使用详解

network | | 访问(45)
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Background="{x:Null}">
<ProgressBar Height="54" HorizontalAlignment="Left" Margin="17,27,0,0" Name="progressBar1" VerticalAlignment="Top" Width="390" IsIndeterminate="True"/>
<!--IsIndeterminate是关键,这个属性确定了ProgressBar的样式,默认是false,就是普通的能显示进度的进度条,true就是不能显示进度的进度条-->
<ProgressBar Height="59" HorizontalAlignment="Left" Margin="31,107,0,0" Name="progressBar2" VerticalAlignment="Top" Width="374" />
<Button Content="更新progressBar2" Height="82" HorizontalAlignment="Left" Margin="63,199,0,0" Name="button1" VerticalAlignment="Top" Width="260"
Click="button1_Click" />
</Grid>
public partial class MainPage: PhoneApplicationPage {
    delegate void ProgressDelegate(int i);
    //声明委托类型
    //委托的内容如有不明白,见http://www.pocketdigi.com/20110916/476.html 有详细注解
    ProgressDelegate progressDelegate;
    //声明委托
    public MainPage() {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e) {
        progressDelegate = SetProgress;
        //把更新进度方法添加进委托
        new Thread(new ThreadStart(ThreadProc)).Start();
        //开启新线程
    }
    private void SetProgress(int i) {
        //这是更新进度条的方法
        progressBar2.Value = i;
        if (i == 100) {
            //如果达到100,则隐藏进度条
            progressBar2.Visibility = Visibility.Collapsed;
            //显示方法Visibility.Visibl
        }
    }
    private void ThreadProc() { //新线程执行的方法
        for (int i = 0; i <= 100; i++) {
            this.Dispatcher.BeginInvoke(progressDelegate, i);
            //线程中调用委托来更新UI,参数是委托,以及委托的参数
            Thread.Sleep(1000);
        }

    }
}
}