2014-02-14 80 views
1

我想延迟Windows Phone 8应用程序中进度条的显示2秒。
因此,如果我在2秒后没有收到响应,则会调用webservice进度条。延迟windows phone 8进度条外观

我已经使用DispatcherTimer实施了代码,但它不能按预期工作。
该变量绑定到IsEnabledIs Progress1ar控件是可见的
问题是这段代码是随机的,而不是在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); 
      } 
     } 
    } 

回答

0

如何使用different 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); 
} 
+0

感谢您的代码工作。 –

+0

@RadenkoZec一旦我看到了你的代码,并注意到它为什么可能不起作用 - 请参阅我的编辑。另一方面,我认为第一个版本更好;) – Romasz

+0

我试图只在代理中更改NotifyOfPropertyChange,但它再次没有奏效。但是,您的代码与System.Threading.Timer轻微更改按预期工作。 –