【发布时间】:2015-10-13 00:44:36
【问题描述】:
我遇到的问题是,每当我通过 MVVM 模型中的命令更改可见性属性以触发加载动画(例如 isBusy = true)时,动画都无法正确播放。结果在运行时是随机的,有时动画非常接近完美,有时它只播放到一半然后循环。
在任何一种情况下,执行此行为总是需要故事板的长度(例如,它会旋转随机的度数,忽略故事板,但总是需要 0.5 秒才能完成。)
奇怪的是,如果我从构造函数触发 isBusy,动画将完美运行,但如果我通过 commandExecute 调用它,它会中断。下面是代码示例和我的 XAML。
<Grid x:Name="LoadingGrid" Visibility="{Binding isBusy, Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay}" Grid.RowSpan="2">
<LoadingViews:LoadingView x:Name="LoadingControl" />
</Grid>
C#:
public StoreSearchViewModel(MainViewModel mainViewModel)
{
this.mainViewModel = mainViewModel;
mainViewModel.LogUsage("Store Search");
searchResultsCommand = new DelegateCommand(SearchResultsCommandExecute);
storeSearchCommand = new DelegateCommand<object>(SetBusy, CanStoreSearchCommandExecute);
CloseWindowCommand = new DelegateCommand(CloseWindowExecute);
Setup();
}
private void SetBusy(object obj)
{
isBusy = true;
}
private bool _isBusy;
public bool isBusy
{
get { return _isBusy; }
set { _isBusy= value; OnPropertyChanged("isBusy"); }
}
上面的代码会导致加载动画出现故障,其中动画位于网格中,其可见性由 isBusy 确定,并由视图中的命令触发。被触发的命令是 storeSearchCommand。
但是下面的代码会产生很好的动画。
private void Setup()
{
//create view models
_storeSearchResultsViewModel = new StoreSearchResultsViewModel(this);
//set default selection to the dashboard
isStoreSearchResultsSelected = true;
SearchResultsCommandExecute();
SetBusy();
}
请注意,“object obj”只是我传递所需的参数来测试代码。请忽略与传递的对象的任何不一致。
我已经为此绞尽脑汁了一段时间,根本无法弄清楚。
【问题讨论】:
-
请发布 CanStoreSearchCommandExecute 的代码。
-
CanStoreSearchCommandExecute 仅检查以确保传递的对象符合某些要求。它不应该以任何方式影响加载动画。 (它会彻底阻止代码运行,如果验证失败,则根本不会播放动画)
-
好的,你能发布一个测试项目的 zip 文件,以便我可以尝试在我的机器上获得相同的结果吗?我在这里尝试过,但没有运气。
标签: c# wpf animation mvvm visibility