我正在应用 MVVM 模式。我有一个按钮,单击该按钮会在我的 ViewModel 中调用委托命令。在该委托方法的一开始,我设置了一个属性值 (WaitOn),它应该通过显示动画控件来通知用户返回 UI 等待。
但是,在委托完成执行之前,显示该动画控件的绑定不会刷新,此时等待已完成。为什么会发生这种情况,我应该怎么做才能解决它?
示例 XAML:
<Button Command="{Binding DoStuffCommand}" />
<ctl:MyAnimatedControl Name="ctlWait" Caption="Please Wait..."
Visibility="{Binding WaitNotification}" />
来自 ViewModel 的片段:
public bool WaitPart1On
{
get { return _waitPart1On; }
set
{
_waitPart1On = value;
if (_waitPart1On == true)
{
WaitNotification = "Visible";
}
else
{
WaitNotification = "Hidden";
}
RaisePropertyChanged("WaitPart1On");
}
}
public string WaitNotification
{
get { return _waitNotification; }
set
{
_waitNotification = value;
RaisePropertyChanged("WaitNotification");
}
}
public void DoStuff()
{
WaitPart1On = true;
//Do lots of stuff (really, this is PART 1)
//Notify the UI in the calling application that we're finished PART 1
if (OnFinishedPart1 != null)
{
OnFinishedPart1(this, new ThingEventArgs(NewThing, args));
}
WaitPart1On = false;
}
现在从 XAML 代码隐藏来捕获引发的事件:
public void Part1FinishedEventHandler(NewThing newThing, ThingEventArgs e)
{
//at this point I expected the WaitPart1On to be set to false
//I planned to put a new wait message up (WaitPart2)
FinishPart2();
}