2014-02-14 158 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輕微更改按預期工作。 –