开发者

Why does my Silverlight BusyIndicator stay invisible after setting IsBusy = true?

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?


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 its DoWork event which runs on a different thread.

myButton_Click(object sender, RoutedEventArgs e)
{
    isbusyindicator1.IsBusy = true;
    var bw = new BackgroundWorker();
    bw.DoWork += (s, args) =>
    {
       //Stuff that takes some time
    };
    bw.RunWorkerCompleted += (s, args) =>
    {
         isbusyindicator1.IsBusy = false;
    };
    bw.RunWorkerAsync();
}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜