2013-10-06 69 views
1

之前,我有必要把我的應用程序的快速暫停執行動作(使應用程序欄按鈕)之前,但我不能確定如何最好地做到這一點。基本上很多處理正在另一個線程中發生,但隨後UI被更新。一旦UI被更新,我將滾動控件滾動到特定的透視項。我想停頓約1秒,或但需要多長時間滾動到擺動控制之前的支點項目,才讓應用程序欄按鈕被激活。我該如何做到這一點?我至今如下如何暫停執行動作

// Show image and scroll to start page if needed 
if (Viewport != null) 
{ 
    Viewport.Source = result; 
    if (editPagePivotControl != null && editPagePivotControl.SelectedIndex != 0) 
    { 
    //Here is where the pivot control is told to move to the first item 
    // (from the second which it will be on before this happens) 
     editPagePivotControl.SelectedIndex = 0; 
    } 
    //A flag to determine whether the app bar button should be enabled 
    //How to pause for the time it takes to finish moving 
    // to the first pivot item?   
    if (_wasEdited) 
     ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true; 
    } 
+0

['System.Threading.Thread.Sleep(/ * milliseconds to wait * /);'](http://msdn.microsoft.com/en-us/library/d00bd51t.aspx) – Sam

+1

@sam oh lawd沒有。只需使用調度程序計時器即可延遲執行特定時間。或者,更好的是,弄清楚如何判斷它何時完成移動。 – Will

+0

@Will嘛總是有[定時器](http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx),但我不認爲這將是值得的OP的情況。 – Sam

回答

1

試試這個,使用System.Windows.Threading.DispatcherTimer

if (_wasEdited) 
{ 
    DispatcherTimer t = new DispatcherTimer(DispatcherPriority.Normal, Dispatcher); 
    t.Tick += new EventHandler((o,e) => 
    ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true; 
    t.Interval = TimeSpan.FromMilliseconds(1000); 
    t.Start(); 
} 

這將等待1000毫秒,然後再回到UI線程(因爲我們在DispatcherTimer設置Dispatcher構造函數)並啓用您的應用程序欄。

如果你這樣做了很多,考慮將計時器類的成員。