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; }
 
        
         
    }
     
}

 

posted on   MyBeN  阅读(1332)  评论(0编辑  收藏  举报

编辑推荐:
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp
· drools 规则引擎和 solon-flow 哪个好?solon-flow 简明教程
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示