繁体   English   中英

延迟Windows Phone 8进度栏的显示

[英]Delay windows phone 8 progress-bar appearance

我想将Windows Phone 8应用程序中的Progressbar的显示延迟2秒钟。
因此,当我致电网络服务时,如果2秒钟后仍未收到响应,则应显示进度栏。

我已经用DispatcherTimer实现了代码,但是无法正常工作。
此变量绑定到ProgressBar控件的IsEnabledIsVisible
问题是此代码是随机运行的,而不是2秒后运行。当我将计时器增加20秒时,即使每个响应都在1秒以下,进度条仍会出现。

 private bool _isProgressBarLoading;
    public bool IsProgressBarLoading
    {
        get
        {
            return _isProgressBarLoading;
        }
        set
        {
            if (_isProgressBarLoading != value)
            {
                if (value)
                {
                    var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(2000) };
                    timer.Tick += delegate
                    {
                        timer.Stop();
                        _isProgressBarLoading = true;
                    };
                    timer.Start();
                }
                else
                {
                    _isProgressBarLoading = false;
                }
                NotifyOfPropertyChange(() => IsProgressBarLoading);
            }
        }
    }

如何在不同的线程上使用不同的Timer操作:

System.Threading.Timer myTimer = null;
private bool _isProgressBarLoading = false;
public bool IsProgressBarLoading
{
    get { return _isProgressBarLoading; }
    set
    {
       if (_isProgressBarLoading != value)
       {
           if (value)
           {
                if (myTimer == null)
                {
                   myTimer = new System.Threading.Timer(Callback, null, 3000, Timeout.Infinite);
                }
                else myTimer.Change(3000, Timeout.Infinite);
                // it should also work if you create new timer every time, but I think it's
                // more suitable to use one
           }
           else
           {
                _isProgressBarLoading = false;
                NotifyOfPropertyChange(() => IsProgressBarLoading);
           }
       }
    }
}

private void Callback(object state)
{
   Deployment.Current.Dispatcher.BeginInvoke(() =>
   {
       _isProgressBarLoading = true;
        NotifyOfPropertyChange(() => IsProgressBarLoading);
    });
}

DispatcherTimer正在处理主线程,我认为使用其他线程会更好。


至于您的代码,如果看起来像这样,它应该可以工作-更改值时通知:

if (value)
{
    var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(2000) };
    timer.Tick += delegate
    {
        timer.Stop();
        _isProgressBarLoading = true;
        NotifyOfPropertyChange(() => IsProgressBarLoading);
    };
    timer.Start();
}
else
{
    _isProgressBarLoading = false;
    NotifyOfPropertyChange(() => IsProgressBarLoading);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM