简体   繁体   English

延迟Windows Phone 8进度栏的显示

[英]Delay windows phone 8 progress-bar appearance

I want to delay appearance of progressbar in Windows Phone 8 application for 2 sec. 我想将Windows Phone 8应用程序中的Progressbar的显示延迟2秒钟。
So when I call webservice if I don't receive response after 2 sec progress-bar should appear. 因此,当我致电网络服务时,如果2秒钟后仍未收到响应,则应显示进度栏。

I have implemented code with DispatcherTimer but it does not seams to work as expected. 我已经用DispatcherTimer实现了代码,但是无法正常工作。
This variable is binded to IsEnabled and IsVisible of ProgressBar control. 此变量绑定到ProgressBar控件的IsEnabledIsVisible
Problem is that this code works randomly and not after 2 sec.When I increase timer for 20 sec progress-bar is still appearing even every response is bellow 1 sec. 问题是此代码是随机运行的,而不是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);
            }
        }
    }

How about using different Timer operating on separate thread: 如何在不同的线程上使用不同的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 is working on the Main thread, I think it will be better to use other Thread. DispatcherTimer正在处理主线程,我认为使用其他线程会更好。


And as for your code it should work if it looks like this - notify when you change the value: 至于您的代码,如果看起来像这样,它应该可以工作-更改值时通知:

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