Here is my diagram for this. My motivation for this is that I do not want the initialization code to work in the user interface thread, and usually I need the initialization code in my App class (and not the splash screen).
Basically, I install the application StartupUriin my splash screen, which receives the ball.
On the splash screen, I call the delegate back to the application. This runs on a workflow. In the splash screen, I process EndInvokeand close the window.
, . Slash, .
, ( ), , .
....
<Application x:Class="SplashScreenDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Splash.xaml">
<Application.Resources>
</Application.Resources>
</Application>
...
internal delegate void Invoker();
public partial class App : Application
{
public App()
{
ApplicationInitialize = _applicationInitialize;
}
public static new App Current
{
get { return Application.Current as App; }
}
internal delegate void ApplicationInitializeDelegate(Splash splashWindow);
internal ApplicationInitializeDelegate ApplicationInitialize;
private void _applicationInitialize(Splash splashWindow)
{
Thread.Sleep(500);
splashWindow.SetProgress(0.2);
Thread.Sleep(500);
splashWindow.SetProgress(0.4);
Thread.Sleep(500);
splashWindow.SetProgress(0.6);
Thread.Sleep(500);
splashWindow.SetProgress(0.8);
Thread.Sleep(500);
splashWindow.SetProgress(1);
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate
{
MainWindow = new Window1();
MainWindow.Show();
});
}
}
splash xaml ( , ...)
<Window x:Class="SplashScreenDemo.Splash"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Splash" Height="300" Width="300">
<Grid>
<TextBlock Height="21" Margin="91,61,108,0" VerticalAlignment="Top">Splash Screen</TextBlock>
<ProgressBar Name="progBar" Margin="22,122,16,109" Minimum="0" Maximum="1"/>
</Grid>
</Window>
...
public partial class Splash : Window
{
public Splash()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Splash_Loaded);
}
void Splash_Loaded(object sender, RoutedEventArgs e)
{
IAsyncResult result = null;
AsyncCallback initCompleted = delegate(IAsyncResult ar)
{
App.Current.ApplicationInitialize.EndInvoke(result);
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate { Close(); });
};
result = App.Current.ApplicationInitialize.BeginInvoke(this, initCompleted, null);
}
public void SetProgress(double progress)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate { progBar.Value = progress; });
}
}
, , , , , .