So I'm trying to have a splash screen that has a progress bar that increments as my application loads. Obviously I've had to edit code to remove any IP, thankfully most of this is simple enough that none of it really matters. But any help would be greatly appreciated.
Failed Solution 1
Here is the code for my application block:
public class App : Application
{
public void Run()
{
bool isLoaded = false;
ProgressSplashScreen splashScreen = new ProgressSplashScreen(() => { isLoaded = true; });
splashScreen.Show();
SpinWait.SpinUntil(() => isLoaded);
_bootStrapper = new BootStrapper(splashScreen);
_bootStrapper.Run();
splashScreen.Close();
}
}
Here is the code for my splash screen XAML:
<Window x:Class="Application.ProgressSplashScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Splash" BorderBrush="Transparent"
AllowsTransparency="True"
Icon="../../Resources/Icons/Icon.ico"
WindowStartupLocation="CenterScreen"
WindowStyle="None" Width="640" Height="520"
Background="Transparent" Name="SplashWindow" Topmost="True" >
<Grid>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Image Source="../../Resources/Images/splash.jpg" Height="480" Width="640"/>
<Grid>
<ProgressBar Name="SplashProgress" Height="20" Minimum="0" Maximum="100"
BorderBrush="Transparent" />
<Label Name="SplashMessage" VerticalAlignment="Bottom"
HorizontalAlignment="Center"/>
</Grid>
</StackPanel>
</Grid>
</Window>
And the Code Behind for the Splash Screen XAML:
using System;
using System.Threading;
using System.Windows;
namespace Application
{
public partial class ProgressSplashScreen : Window
{
private SynchronizationContext _synchContext;
public ProgressSplashScreen(Action isLoaded)
{
this.Loaded += (sender, args) => isLoaded();
InitializeComponent();
_synchContext = SynchronizationContext.Current;
}
public void SetProgress(double progress, string message)
{
_synchContext.Send((state) =>
{
SplashProgress.Value = progress;
SplashMessage.Content = message;
}, null);
}
}
}
And Finally the BootStrapper Class that I am trying to make the update calls from:
namespace Application
{
internal sealed class BootStrapper : BaseMefBootstrapper
{
private ProgressSplashScreen _splash;
public BootStrapper(ProgressSplashScreen splashScreen)
{
_splash = splashScreen;
}
public void Run()
{
// removed do stuff code here
_splash.SetProgress(10, "Loading stuff...");
// removed do stuff code here
_splash.SetProgress(40, "Loading stuff...");
// removed do stuff code here
_splash.SetProgress(80, "Loading stuff...");
// removed do stuff code here
_splash.SetProgress(90, "Loading stuff...");
// removed do stuff code here
_splash.SetProgress(100, "Loading stuff...");
}
}
}
And so far my screen just sits there with an empty progress bar...
Failed Solution 2
ps... I've also tried
using System;
using System.Threading;
using System.Windows;
namespace Application
{
public partial class ProgressSplashScreen : Window, INotifyPropertyChanged
{
private Dispatcher _dispatcher;
public event PropertyChangedEventHandler PropertyChanged;
private string _progressMessage;
private double _progressValue;
public string ProgressMessage
{
get { return _progressMessage; }
set
{
_progressMessage = value;
OnPropertyChanged("ProgressMessage");
}
}
public double ProgressValue
{
get { return _progressValue; }
set
{
_progressValue = value;
OnPropertyChanged("ProgressValue");
}
}
public ProgressSplashScreen(Action isLoaded)
{
this.Loaded += (sender, args) => isLoaded();
InitializeComponent();
_dispatcher = Dispatcher.CurrentDispatcher;
}
public void SetProgress(double progress, string message)
{
_dispatcher.BeginInvoke(new Action(() =>
{
ProgressValue = progress;
ProgressMessage = message;
}), DispatcherPriority.ContextIdle, null);
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
with this as the XAML
<Window x:Class="Application.ProgressSplashScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Splash" BorderBrush="Transparent"
AllowsTransparency="True"
Icon="../../Resources/Icons/Icon.ico"
WindowStartupLocation="CenterScreen"
WindowStyle="None" Width="640" Height="520"
Background="Transparent" Name="SplashWindow" Topmost="True" >
<Grid>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<Image Source="../../Resources/Images/splash.jpg" Height="480" Width="640"/>
<Grid>
<ProgressBar Name="SplashProgress" Height="20" Minimum="0" Maximum="100"
BorderBrush="Transparent" Value="{Binding ProgressValue,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<Label Name="SplashMessage" VerticalAlignment="Bottom" HorizontalAlignment="Center"
Content="{Binding ProgressMessage,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Grid>
</StackPanel>
</Grid>
</Window>
Any help would be great! Thanks! Really surprised this is as hard as it seems right now...
I hope it could help you.
BackgroundWorkerand set the value of the progress bar withProgressChanged? - Bastien