tags:

views:

504

answers:

2

Hi folks,

I have a function in my silverlight app, that takes one or two seconds to finish. While executing this function, I want to show my "just loading" UC to the user:

private void ComboBoxGranularity_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
  WaitScreenObject.Visibility = Visibility.Visible;

  OperationThatTakesALittleTime();

  WaitScreenObject.Visibility = Visibility.Collapsed;
}

Problem is, that Silverlight doesn't redraw while the function is executed, so my WaitScreen doesn't show up. I tried the trick from this question:

this.Visibility = Visibility.Visible;

but it didn't work. I wanted to avoid the Backgroundworker-Overhead, so is there any possibilty to make the WaitScreenObject visible?

Thanks in advance, Frank

A: 

Have you tried invalidating the visual for the root object to try convince it to redraw? (Havn't checked if it would work, just comes to mind as a hack)

Edit : InvaldiateVisual is the method in WPF on the UIelement, in Silverlight you use a diff call, such as InvalidateArrange

myCanvasRoot.InvalidateArrange();

Bit hacky but might convince it to perform the update of the screen.

Andrew
+2  A: 

Your method runs on the UIThread since you mentioned that you are not using a background thread. The thread only redraws the screen when it is not busy with something else, since the queue is filled with the other instructions, and those are considered more important than a redraw.

I tried what Andrew suggested, but I could not find the InvalidateVisual() method on the UIElement. Maybe I was just being daft.

The reason why the linked example did not work for you, is because the other question just dealt with an element not being invalidated because it did not have focus. However, the UIThread was available for a refresh at that time.

I also tested the dispatcher.BeginInvoke() on a delegate, and it did not work either. I am afraid that from my point of view, you might just have to use a separate thread.

I could be wrong because I simulated my "work" by making the thread sleep instead, however, I cannot see what the difference will be.

Hope this helps.

Johannes
I could not find the InvalidateVisual() too. I tried it with InvalidateArrange(), which should also force an asynchronous redraw ... but it did not work either. Looks like I have to call my function asynchronously.
Aaginor
Sorry, I used the WPF method name InvalidateVisual, the silverlight UIelement doesn't have it, it has InvalidateArrange or InvalidateMeasure but doesn't look like it will work either from Johannes test.
Andrew