调用Method ExportStarts()时,我想开始动画。然后我在逻辑单元(Manager.StartExport())中调用另一个方法,在那里我使用Background Worker进行操作。完成后,我想回到视图并停止动画。我怎么能这样做?
View.xaml.cs
if (...)
{
storyboard.Begin();
List<TaskResult> Results = manager.StartExport();
storyboard.Stop();
}
manager.StartExport()
public static List<TaskResult> StartExport()
{
bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(worker_Do);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunCompleted);
bw.RunWorkerAsync();
return Results;
}
提前致谢
答案 0 :(得分:0)
你想在worker_RunCompleted yes之后停止故事板吗?
检查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
}