I'm making a splash screen in App.cs. When I use SplashWorker.RunWorkerAsync();
DataTemplate not appeared.
but when annotating this code, DataTemplate was appeared....
It is properly registered ViewModel and ResourceDictionary.
App.xaml
<Application .......... Startup="Application_Startup">
App.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
SplashWindow sw = new SplashWindow();
sw.Closed += (ss, ee) =>
{
if (ServiceLocator.Current.GetInstance<SplashViewModel>().ClosedByUser)
{
this.Shutdown();
}
ServiceLocator.Current.GetInstance<SplashViewModel>().Cleanup();
sw = null;
};
if ((bool)sw.ShowDialog())
{
this.ShutdownMode = ShutdownMode.OnMainWindowClose;
MainWindow = new MainWindow();
MainWindow.Show();
}
}
SplashViewModel.cs
public SplashViewModel()
{
SplashWorker = new BackgroundWorker();
SplashWorker.WorkerSupportsCancellation = true;
SplashWorker.DoWork += SplashWorker_DoWork;
SplashWorker.RunWorkerCompleted += SplashWorker_RunWorkerCompleted;
Result = null;
SplashWorker.RunWorkerAsync();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
SplashWindow sw = new SplashWindow();
sw.Closed += (ss, ee) =>
{
if (ServiceLocator.Current.GetInstance<SplashViewModel>().ClosedByUser)
{
this.Shutdown();
}
ServiceLocator.Current.GetInstance<SplashViewModel>().Cleanup();
sw = null;
};
sw.Loaded += (ss, ee) =>
{
ServiceLocator.Current.GetInstance<SplashViewModel>().RunWorker();
};
if ((bool)sw.ShowDialog())
{
this.ShutdownMode = ShutdownMode.OnMainWindowClose;
MainWindow = new MainWindow();
MainWindow.Show();
}
}
As mentioned by @AnjumSKhan in the comments ,move your SplashWorker
code in Startup
event of the Application because DataTemplate would work while binding, which happens after InitializeComponent
method.