views:

2241

answers:

4

Hi ! I Have a ListView with many Items that is to be loaded in search. And I'd like to provide user the richer user interface so that when loading, I'd display a rotating circle (known from AJAX waiting).

I realize that I'll have to go into Threads or something, but as I've never done this before in WPF, I'm sure there is something better than Threads in WPF (or a simpe BackgroundWorker).

Anyways, the point is to display that animation while loading. Any idea? Thanks!

A: 

Use Dispatcher.Invoke method.

adatapost
OK. But what if a user wants to cancel the search? How could I interrupt the receiving process from a remote sql server?
PaN1C_Showt1Me
This also doesn't help with his main question - how to display a loading animation.
Anderson Imes
A: 

I found an Article talking right about this:

http://msdn.microsoft.com/en-us/library/ms741870.aspx

PaN1C_Showt1Me
+1  A: 

OK I've got a working solution now.

I have a UserControl that contains that animation. It is located somewhere in my XAML code like this: <customControls:LoadingAnimation x:Name="LoadingAnimation" />. This control is loaded when neede by calling

LoadingAnimation.Show();

Now when I click on a Button to do the time-consuming work, before I call BeginInvoke() I load that animation.

Then when the hard work is finished I call LoadingAnimation.Hide(). Very simple! I add code for the others:

private void SearchClick(object sender, RoutedEventArgs e)
{
     LoadingAnimation.Show();

     new StringDelegate(DoSearch).BeginInvoke("TextToSearch", null, null);
}

private void DoSearch(string searchText)
{
    object result = /* Do the time consuming work */    

    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
         new ResultDelagate(UpdateUserInterface), result);
}

private void UpdateUserInterface(object result)
{
    LoadingAnimation.Hide();            

    DataContext = result as /* what you want */;
}
PaN1C_Showt1Me
Just curious, how did you solve the canceling part ?
cwap
Just have a status, IsSearching, and use datatriggers to show and hide the user control
TerrorAustralis
A: 

StringDelegate and ResultDelagate doesn't exist. How to get them ?

Gil