【发布时间】:2012-11-16 15:18:05
【问题描述】:
我有一个 BusyIndicator,我将 IsBusy 绑定到我的视图模型中的 Busy 属性。
<xctk:BusyIndicator IsBusy="{Binding Busy}" x:Name="busyBox" Grid.Row="2"
HorizontalAlignment="Center"
VerticalAlignment="Center" BusyContent="Contacting Server" >
</xctk:BusyIndicator>
我在启动 web 服务调用(异步)时将忙切换为 true,并在回调中将其设置为 false。
第一次效果很好,之后每次都不再显示忙碌指示符。我在回调中添加了一个 thread.sleep (只是在 x=case 它第二次移动得太快了)。
我知道我的属性正在正确通知,因为其他绑定控件并按预期工作。似乎busyindicator只适用于一次使用
(顺便说一句,我正在使用 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
}
这是访问 web 服务的类
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();
}
}
用户界面上的其他所有内容都表现得很好。我的按钮正确激活/停用。我的网格得到很好的更新等等等等
【问题讨论】:
-
你能发布你的回调视图模型的代码吗?
-
如果 BusyIndicator 控件是问题所在,您始终可以创建自己的控件。它并不像看起来那么难(您只需将 Busy 指示器和 View 内容放在一个没有行的网格中,并将指示器的 Visibility 绑定到您的 Busy 属性)。
-
我认为这不是 BusyIndicator,可能是异步线程问题。也许与 WPF 的视觉线程有关。也许它使用
SynchronizationContext将IsBusy属性设置回false 来解决。 -
还必须添加 DataContext 项的代码。
-
@RaulOtaño 我认为你是对的。我做了一个没有异步的小示例应用程序,指标的行为与我预期的一样。我正在使用 backgroundWorker 来做我的网络服务调用。在 DoWork() 方法中,busy 设置为 true,在传递类并由 RunWorkerCompleted() 方法启动的回调方法中,busy 设置为 false。进程在 RunWorkerAsync() 中触发。我认为 RunWorkerCompleted 自动在 UI 线程内运行?还是我应该做一些特别的事情?生病发布一些示例代码
标签: wpf mvvm-light wpftoolkit