创建全局SystemTray.ProgressIndicator

当处理耗时任务时,我们自然要给个进度指示器让用户看到应用正在做事呢,不然用户就会想到底好没好啊,死了?。所以我们一定要有进度指示器,这样才是一个正常的用户体验。而创建进度指示器的最简单方法就是使用系统托盘里的ProgressIndicator,不过系统托盘和页面是挂钩的,要是用户这时转入其它页面,那ProgressIndicator就没了啊,用户就看不到进度了。所以不管用户做了什么事,ProgressIndicator都要显示出来,就是要做个全局的ProgressIndicator。于是网上搜索一番,找到了个代码,思路是这样的:写一个类,保存ProgressIndicator的相关数据,订阅Frame的Navigating和Navigated,监视页面切换。对移入的页面,利用保存的数据创建一个ProgressIndicator显示出来,对移出的页面,隐藏ProgressIndicator。不过后来想了想,能不能用绑定来实现呢?于是试了一下,果真可以。下面是相关代码:

using System;
using System.ComponentModel;
namespace *.ViewModels
{
    public class ProgressIndicatorViewModel : INotifyPropertyChanged
    {
        private Boolean isVisible;
        private String text;
        private Boolean isIndeterminate;
        private Double value;

        /// <summary>
        /// 获取或设置 是否显示ProgressIndicator。
        /// </summary>
        public Boolean IsVisible
        {
            get
            {
                return isVisible;
            }
            set
            {
                if (isVisible != value)
                {
                    isVisible = value;
                    NotifyPropertyChanged("IsVisible");
                }
            }
        }
        /// <summary>
        /// 获取或设置 显示的文字。
        /// </summary>
        public String Text
        {
            get
            {
                return text;
            }
            set
            {
                if (text != value)
                {
                    text = value;
                    NotifyPropertyChanged("Text");
                }
            }
        }
        /// <summary>
        /// 获取或设置 进度指示器是不确定的还是确定的。
        /// </summary>
        public Boolean IsIndeterminate
        {
            get
            {
                return isIndeterminate;
            }
            set
            {
                if (isIndeterminate != value)
                {
                    isIndeterminate = value;
                    NotifyPropertyChanged("IsIndeterminate");
                }
            }
        }
        /// <summary>
        /// 获取或设置 进度(0-1)。
        /// </summary>
        public Double Value
        {
            get
            {
                return value;
            }
            set
            {
                if (this.value != value)
                {
                    this.value = value;
                    NotifyPropertyChanged("Value");
                }
            }
        }

        public ProgressIndicatorViewModel()
        {
            isVisible = false;
            text = String.Empty;
            isIndeterminate = false;
            value = 0;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            if (null != PropertyChanged)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
<phone:PhoneApplicationPage
    shell:SystemTray.Opacity="0"
    shell:SystemTray.IsVisible="{Binding IsVisible}">
    
    <shell:SystemTray.ProgressIndicator>
        <shell:ProgressIndicator IsVisible="{Binding IsVisible}"
                                 Text="{Binding Text}"
                                 IsIndeterminate="{Binding IsIndeterminate}"
                                 Value="{Binding Value}"/>
    </shell:SystemTray.ProgressIndicator>

    <Grid>
    </Grid>

</phone:PhoneApplicationPage>

 

 

posted on 2013-04-29 19:23  snet  阅读(1346)  评论(1编辑  收藏  举报