2014-03-13 76 views
1

我實際上有2個類似的問題在這裏沒有運氣在網上找到任何東西。等待UI重新渲染完成

問題1:使用BackgroundWorker,我正在用%完成UI更新,但我使用UserState,因爲我想報告百分比的小數。問題在於,根據輸入的不同,有時更新很少發生(每隔幾秒鐘一個百分比),而其他時間則非常快(每秒鐘觸發小數%更新多次)。在後一種情況下,我得到一個堆棧溢出(無雙關語意思)問題。我猜ProgressChanged事件太多了。這是現在的原型代碼,我直接在progressChanged事件中更新TextBlock,而不是使用ViewModels,但我會在稍後。不知道這是否是問題。有沒有辦法允許這個進度改變的事件被稱爲它需要的頻率,但是可以這樣說: if(!mytextblock.IsRendering()) mytextblock.text = newPercent;

這樣它就會在完成繪製最後一個數字時更新。如果百分數跳過,沒關係。

問題2:這是一個個人項目,我正在拍攝屏幕,以某種方式更改屏幕,然後在wpf程序中顯示更改後的圖像,並不斷重複。有沒有辦法說: GrabScreen EditImage UpdateUI WaitForUIToRender // < -------我該怎麼做? 重複

謝謝

+0

創建相應的視圖模型,並使用正確的數據綁定和你所有的問題會奇蹟般地消失了。 –

+0

嘗試過,但仍然遇到同樣的問題 – wormiii

+0

發佈您當前的代碼和XAML以及適合您的當前問題的具體詳細解釋。 –

回答

0
public class TimedAction 
{ 
    public static void ExecuteWithDelay(Action action, TimeSpan delay) 
    { 
     var timer = new DispatcherTimer(); 
     timer.Interval = delay; 
     timer.Tag = action; 
     timer.Tick += timer_Tick; 
     timer.Start(); 
    } 

    static void timer_Tick(object sender, EventArgs e) 
    { 
     var timer = (DispatcherTimer)sender; 
     var action = (Action)timer.Tag; 

     action.Invoke(); 
     timer.Stop(); 
    } 

    public static void ExecuteAfterRendering(Action action) 
    { 
     ExecuteAfterFrames(action, 3); 
    } 

    public static void ExecuteAfterFrames(Action action, int frames) 
    { 
     var timedAction = new TimedAction(); 
     timedAction._currentAction = action; 
     timedAction._framesToWait = frames; 
    } 

    private Action _currentAction; 
    private int _framesToWait; 
    private int _currentFrame = 0; 
    private TimedAction() 
    { 
     CompositionTarget.Rendering += CompositionTarget_Rendering; 
    } 

    private void Dispose() 
    { 
     CompositionTarget.Rendering -= CompositionTarget_Rendering; 
    } 

    void CompositionTarget_Rendering(object sender, EventArgs e) 
    { 
     _currentFrame++; 
     if (_currentFrame == _framesToWait) 
     { 
      _currentAction.Invoke(); 
      Dispose(); 
     } 
    } 

}