我有一个BusyIndicator,我将IsBusy绑定到我的视图模型中的Busy属性。
<xctk:BusyIndicator IsBusy="{Binding Busy}" x:Name="busyBox" Grid.Row="2"
HorizontalAlignment="Center"
VerticalAlignment="Center" BusyContent="Contacting Server" >
</xctk:BusyIndicator>
我在启动一个Web服务调用(异步)时将busy设置为true,并在回调函数中将其设置为false。
第一次这样做非常好,但之后每次都不再显示忙指示器。我在回调函数中添加了一个thread.sleep(只是为了防止第二次移动得太快)。
我知道我的属性通知正确,因为其他绑定的控件按预期工作。只是似乎忙指示器仅适用于一次使用
(顺便说一句,我正在使用MVVM Light Toolkit v3)
视图模型代码
this.Busy = true; //This proverty is declared correctly with notifications etc
IPersonSearchService searcher = new PersonSearchService(); //class that does my webservice, ad i pass it a callback method from my UI (see below)
searcher.FindByPersonDetails(ps, GetAllPeopleCallback);
private void GetAllPeopleCallback (PersonSearchResult result, Exception e)
{
this.Busy = false;
((Models.PersonSearch)this.Model).Persons = result.Persons; //bound to my grid
CommandManager.InvalidateRequerySuggested(); //i need to do this to make a button who's canexecute command binding happen
}
这是调用网络服务的类。
class PersonSearchService : IPersonSearchService
{
public void FindByPersonDetails(WSPersonSearch.PersonSearch ps, Action<PersonSearchResult, Exception> Callback)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
WSPersonSearch.PersonSearch search = (WSPersonSearch.PersonSearch)args.Argument;
PersonSearchWebServiceClient wc = new PersonSearchWebServiceClient();
PersonSearchResult r = wc.FindByPersonDetails(ps);
args.Result = r;
};
worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
PersonSearchResult result = (PersonSearchResult)args.Result;
Callback(result, null);
};
worker.RunWorkerAsync();
}
}
UI上的其他所有内容都表现良好。我的按钮能够正确激活/停用。我的网格得到了良好的更新等等。