2015-09-23 266 views
0

當調用方法ExportStarts()時,我想要啓動動畫。然後我調用邏輯單元(Manager.StartExport())內的另一個方法,在那裏我使用Background Worker來做東西。完成後,我想返回視圖並停止動畫。我怎樣才能做到這一點?當BackgroundWorker完成時停止故事板

View.xaml.cs

if (...) 
{ 
storyboard.Begin();     
List<TaskResult> Results = manager.StartExport(); 
storyboard.Stop(); 
} 

manager.StartExport()提前

+0

你已經回答了你自己的問題,不是嗎?你有你需要的一切..... – kevintjuh93

回答

0

public static List<TaskResult> StartExport() 
    { 
     bw = new BackgroundWorker(); 
     bw.DoWork += new DoWorkEventHandler(worker_Do); 
     bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunCompleted); 
     bw.RunWorkerAsync(); 

     return Results; 
    } 

感謝您想worker_RunCompleted後停止故事板是嗎?

檢查TaskCompletionSource :)

如果我理解正確的 - 這將是有益的。

_storyboard.Stop();_storyboardTaskCompletionSource的結果設置爲RunWorkerCompleted後引發。

private TaskCompletionSource<bool> _storyboardTaskCompletionSource; 
Storyboard _storyboard = new Storyboard(); 

private async Task InitAsync() 
{ 
    _storyboardTaskCompletionSource = new TaskCompletionSource<bool>(); 
    _storyboard.Begin(); 

    StartProgress(); 

    await _storyboardTaskCompletionSource.Task; 

    _storyboard.Stop(); 
} 

public void StartProgress() 
{ 
    var bw = new BackgroundWorker(); 
    bw.DoWork += DoWork; 
    bw.RunWorkerCompleted += RunWorkerCompleted; 
    bw.RunWorkerAsync(); 
} 

private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    _storyboardTaskCompletionSource.SetResult(true); 
} 

private void DoWork(object sender, DoWorkEventArgs e) 
{ 
    //logic 
}