簡體   English   中英

在MVVM中等待動畫完成的最佳方法是什么?

[英]What is the best way to wait for an animation to finish in MVVM?

我有一個帶有周期性動畫的MetroTile(用C#代碼編寫)和一個DataGrid。 當網格有驗證錯誤時,viewmodel中的tile命令的CanExecute()返回false並禁用該tile。

這是在計時器的tick方法中執行的動畫代碼:

DoubleAnimation db = new DoubleAnimation(startStopTile.ActualHeight, 0, TimeSpan.FromSeconds(0.5));
db.FillBehavior = FillBehavior.Stop;
startStopTile.BeginAnimation(HeightProperty, db);

問題是可以在動畫期間禁用磁貼並在其中間凍結。 在禁用磁貼之前等待動畫完成的最佳方法是什么?

我認為我可以在動畫結束時更新一個viewmodel屬性(即bool AnimationCompleted )並在CanExecute()中等待一個while循環使其轉為true ,但我不確定它是否是最好的方法。

為什么不在動畫完成后使用動畫完成事件通知ViewModel 循環時無需等待(這很糟糕,因為它會阻止UI)。

db.Completed += OnAnimationComplete;

private void OnAnimationComplete(object sender, EventArgs e)
{
    db.Completed -= OnAnimationComplete;

    // Notify ViewModel that it finished
    //
    // example: viewModel.NotifyAnimationComplete();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM