为什么在设置 IsBusy = true 后我的 Silverlight BusyIndicator 保持不可见?
当 DomainDataSource 获取数据时,我的 BusyIndicator 按预期工作。但是当我在代码中将 IsBusy 设置为 true 时,该控件保持不可见。
我正在使用 Silverlight 4 和 BusyIndicator 工具包。在 XAML 中,我已将 BusyIndicator.IsBusy 属性绑定到 DomainDataSource 控件的 IsBusy 属性。 BusyIndicator 控件包装了我的主 Grid (LayoutRoot) 和所有子控件。
<toolkit:BusyIndicator IsBusy="{Binding ElementName=labSampleDomainDataSource, Path=IsBusy}" Name="busyIndicator1">
<Grid x:Name="LayoutRoot">
</Grid>
</toolkit:BusyIndicator>
问题是当我设置 busyIndicator1 = true; 时 BusyIndicator 不显示;在按钮单击事件中。知道我做错了什么吗?
My BusyIndicator works as expected when the DomainDataSource fetches data. But the control stays invisible when I set IsBusy to true in code.
I'm using Silverlight 4 and the toolkit BusyIndicator. In XAML I have bound the BusyIndicator.IsBusy property to the IsBusy property of my DomainDataSource control. The BusyIndicator control wraps my main Grid (LayoutRoot) and all child controls.
<toolkit:BusyIndicator IsBusy="{Binding ElementName=labSampleDomainDataSource, Path=IsBusy}" Name="busyIndicator1">
<Grid x:Name="LayoutRoot">
</Grid>
</toolkit:BusyIndicator>
The problem is that the BusyIndicator does not show when I set busyIndicator1 = true; in a button click event. Any idea what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UI 在 UI 线程上非常自然地更新自身。按钮单击等事件也在 UI 线程上运行。
在某些情况下,属性更改和对控件的方法调用将导致方法被分派到 UI 线程。这意味着要调用的方法在 UI 线程可用于执行该方法之前不会实际发生(并且已执行排队的其他任何内容)。
IsBusy
属于此类。只有当您的代码完成 UI 线程时,UI 才有机会更新其外观。您不应该在 UI 线程中执行任何长时间运行的任务。在你的情况下,你绑定线程来执行你自己的命令,并让 UI 缺乏可用来完成工作的线程。
相反,您应该使用
BackgroundWorker
类,并在其在不同线程上运行的DoWork
事件中完成所有繁重的工作。现在,此单击事件完成得非常快,允许使用 UI 线程来更新 UI,而实际工作则在后台线程上完成。完成后,IsBusy 被清除。
The UI updates itself quite naturally on the UI Thread. Events for things like button clicks also run on the UI thread.
In some cases property changes and method calls into controls will result in a method being dispatched to the UI thread. What this means is that the method to be invoked will not actually occur until the UI thread becomes available to execute it (and anything else already queued up has been executed).
IsBusy
falls into this category.Only when your code has finished with the UI thread will the UI get a chance to update its appeareance. You should not be doing any long running tasks in the UI thread. In your case you tie up the thread to do your own bidding and leave the UI starved of this one thread it can use to get its work done.
Instead you should look to use the
BackgroundWorker
class and do all the heavy work in itsDoWork
event which runs on a different thread.Now this click event completes very quickly allowing the UI thread to be used to update the UI whilst the actual work is done on a background thread. When completed the IsBusy is cleared.