Wpf Loading Spinner

WPF loading spinner

I wrote this user control which may help, it will display messages with a progress bar spinning to show it is currently loading something.

  <ctr:LoadingPanel x:Name="loadingPanel"
IsLoading="{Binding PanelLoading}"
Message="{Binding PanelMainMessage}"
SubMessage="{Binding PanelSubMessage}"
ClosePanelCommand="{Binding PanelCloseCommand}" />

It has a couple of basic properties that you can bind to.

What is the best way to display a 'loading' indicator on a WPF control

I generally would create a layout like this:

<Grid>
<Grid x:Name="MainContent" IsEnabled="False">
...
</Grid>

<Grid x:Name="LoadingIndicatorPanel">
...
</Grid>
</Grid>

Then I load the data on a worker thread, and when it's finished I update the UI under the "MainContent" grid and enable the grid, then set the LoadingIndicatorPanel's Visibility to Collapsed.

I'm not sure if this is what you were asking or if you wanted to know how to show an animation in the loading label. If it's the animation you're after, please update your question to be more specific.

How to show 'Loading...' overlay while the View Model reloads the bound data

I quickly came to the conclusion that simply switching a boolean flag bound to the UI, before and after the operation doesn't work. The UI does not get refreshed until the entire operation completes. Maybe because the operation is CPU-intensive, I don't know.

Yes, it should work provided that you are actually executing the long-running operation on a background thread.

Please refer to the following simple example.

View:

<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
xmlns:s="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="300">
<Window.DataContext>
<local:Window1ViewModel />
</Window.DataContext>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<Grid>
<TextBlock>Content...</TextBlock>
<Grid Background="Yellow" Visibility="{Binding IsLoading, Converter={StaticResource BooleanToVisibilityConverter}}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Loading...</TextBlock>
</Grid>
</Grid>
</Window>

View Model:

public class Window1ViewModel : INotifyPropertyChanged
{
public Window1ViewModel()
{
IsLoading = true;
//call the long running method on a background thread...
Task.Run(() => LongRunningMethod())
.ContinueWith(task =>
{
//and set the IsLoading property back to false back on the UI thread once the task has finished
IsLoading = false;
}, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
}

public void LongRunningMethod()
{
System.Threading.Thread.Sleep(5000);
}

private bool _isLoading;
public bool IsLoading
{
get { return _isLoading; }
set { _isLoading = value; NotifyPropertyChanged(); }
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

Using a Loading Indicator for WPF application

That key exists in the Styles.xaml file. So add a reference to it in your resources.

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/LoadingIndicators.WPF;component/Styles/LoadingDoubleBounce.xaml"/>
<ResourceDictionary Source="pack://application:,,,/LoadingIndicators.WPF;component/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

Displaying spinner waiting indicator while loading web page

Yes, use a Frame and handle the LoadCompleted event. In other words, show the spinner until LoadCompleted fires.

There is no need to use a separate thread for loading: The Frame control already provides for loading the content in the background.

How to show Spinning Modal in WPF

You should perform the action on a background thread. The UI thread cannot both display the spinner and run your code at the same time:

SpinningModalVisibility = Visibility.Visible;
Task.Factory.StartNew(()=>
{
//YourAction();
}).ContinueWith(Task =>
{
SpinningModalVisibility = Visibility.Hidden;
}, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

Waiting Screen for WPF window Loading

You can subscribe your background worker to report progress.

worker.WorkerReportsProgress = true;

And now you can have this progress report be triggered by an event you subscribe to.

worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

In doing so, you can create a progress bar than can update itself based on this worker_ProgressChanged event, triggered by your computation.

It appears you've already figured out IsBusy, so you can have your progress bar show only when this is true.



Related Topics



Leave a reply



Submit