WIN8开发中使用进度条

之前总结并撰写了一篇关于在WP7中使用进度条的文章:【原创】WP7中使用进度条——弥补几乎所有网络上与之相关的技术博文的内容缺陷

随后探索了一下在WIN8应用开发中使用进度条的方法,与WP7相比还是有一些区别的。下面介绍WIN8开发中如何使用进度条,还是给出程序框架,并将部分代码段进行高亮以突出同WP7中代码的区别。


这篇文章以文章开头给出的那篇文章为依托进行讲解,使用同样的例子,因此,在阅读本文之前,请务必先移步【原创】WP7中使用进度条——弥补几乎所有网络上与之相关的技术博文的内容缺陷 进行相关故事梗概的了解。

依然假设要在对某一图像进行处理的过程中加入进度条显示。下面直接给出程序框架。


必要的引用集:

using System.Threading.Tasks;
using Windows.System.Threading;
using Windows.UI.Core;


进度条相关的XAML代码:

<ProgressBar Height="27" Foreground="Teal" HorizontalAlignment="Left" Margin="66,460,0,0" Name="progressBar" VerticalAlignment="Top" Width="255"/>  
<TextBlock Height="30" Foreground="#FFD61111" HorizontalAlignment="Left" Margin="326,460,0,0" Name="percentage" Text="" VerticalAlignment="Top" /> 


程序框架:

public partial class xxxx : PhoneApplicationPage  
{  
    public xxxx()  
    {  
        InitializeComponent();  
    }  
    private void btn_Click(object sender, RoutedEventArgs e)  
    {  
        progressBar.Visibility = Visibility.Visible;//点击按钮之前为了界面美观,进度条隐藏,进入此函数后使进度条可见  
        progressBar.Value = 0;                      //初始值设为0  
        percentage.Visibility = Visibility.Visible;
        ThreadPool.RunAsync(New_thread);          //这是WIN8中开启新线程的方法,WP7中的对应代码为:new Thread(new ThreadStart(New_thread)).Start();    
    }  
  
    //下面是在新开启的线程中对图像进行处理的代码框架。依旧假定这里的图像处理分3个阶段完成,分别由函数stage_1(),stage_2()和stage_3()实现。
    //下面的代码中也对WIN8中特有的await方法进行了解说,见本函数的尾部。
    private async void New_thread()               //特别留意关键字:async  下面代码中用到await方法,函数名前面必须有此关键字。这与WP7中是不一样的。
    {  
        stage_1();
	//接下来贴出另一处WIN8与WP7代码不一样的地方
        for (int i = 0; i <= 30; i++)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>     //WP7中这里是:Dispatcher.BeginInvoke(() => 
            {
                progressBar.Value = i;
                percentage.Text = progressBar.Value.ToString() + "%";
            }); 
            new System.Threading.ManualResetEvent(false).WaitOne(10);          //WP7中这里是:Thread.Sleep(10);
        }
  
        stage_2();
        for (int i = 31; i <= 70; i++)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>     //WP7中这里是:Dispatcher.BeginInvoke(() => 
            {
                progressBar.Value = i;
                percentage.Text = progressBar.Value.ToString() + "%";
            }); 
            new System.Threading.ManualResetEvent(false).WaitOne(10);          //WP7中这里是:Thread.Sleep(10);
        }
  
        stage_3();
        for (int i = 71; i <= 100; i++)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>     //此行在WP7中对应的语句是:Dispatcher.BeginInvoke(() => 
            {
                progressBar.Value = i;
                percentage.Text = progressBar.Value.ToString() + "%";
            }); 
            new System.Threading.ManualResetEvent(false).WaitOne(10);          //此行在WP7中对应的语句是:Thread.Sleep(10);
        }
        
        //如果你在这个新线程调用中想写出类似下面的语句,即:new一个writeableBitmap的图像img,留着在后面的代码中用,那么,你需要如下做即可
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>         //注意开始的这个await关键字
        {
            writeableBitmap img = new WriteableBitmap(600, 400);
        });

        //  下面我贴出WP7中实现同样功能的代码段,大家看看它们的区别。
        //  可以看到,WIN8中的await方法自动实现了线程等待,可以不再写类似于WP7中的线程等待语句Thread.Sleep(100)
        //  Dispatcher.BeginInvoke(() =>                          //区别 1
        //  {      
        //      writeableBitmap img = new writeableBitmap(600, 400);  
        //  }); 
        //  Thread.Sleep(100);   //线程等待                       //区别 2,WP7需要有实现线程等待的语句,WIN8中有await关键字就够啦
    }  
    //……  
}

这里再对WP7中使用进度条补充一点:

如果大家对WP7中的那个委托机制感觉不爽,也可以不用它。直接把委托相关的代码删掉,然后类似上面WIN8代码中的New_thread()函数那样即可,也就是说,找到New_thread()中的类似下面的代码段:

        //....
        for (int i = 71; i <= 100; i++)
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>     //此行在WP7中对应的语句是:Dispatcher.BeginInvoke(() => 
            {
                progressBar.Value = i;
                percentage.Text = progressBar.Value.ToString() + "%";
            }); 
            new System.Threading.ManualResetEvent(false).WaitOne(10);          //此行在WP7中对应的语句是:Thread.Sleep(10);
        }
        //....

按照这个写法,WP7中的对应代码段如下:

        //....
        for (int i = 71; i <= 100; i++) 
        {  
            this.Dispatcher.BeginInvoke(progressDelegate, i);    //删掉这句!
            Dispatcher.BeginInvoke(() =>  
            {  
                progressBar.Value = i;                           //加上这句!      
                percentage.Text = progressBar.Value.ToString() + "%";  
            });  
            Thread.Sleep(10);  
        }
	//....

这样便可以避免去使用委托机制。如果想在进度条到达100%的时候将其隐藏起来,只需要在New_thread()函数的最后加上如下代码即可:

WIN8中的写法:

            //....
            Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                progressBar.Visibility = Visibility.Collapsed;
                percentage.Visibility = Visibility.Collapsed;
            });
            //....


当然,WP7中如何写,也明白了吧:

            //....
            Dispatcher.BeginInvoke(() =>  
            { 
                progressBar.Visibility = Visibility.Collapsed; 
                percentage.Visibility = Visibility.Collapsed; 
            }); 
            //....

 

上面就是对WIN8中使用进度条的方法。同时,也将其同WP7中使用进度条进行了相似代码段的比较,也可为大家将应用在两个平台之间进行移植提供参考和帮助。


关键字:WIN8中使用进度条, WIN8中开启新线程, WIN7中使用进度条


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值