DataTemplate 未出现在 Application_Startup

DataTemplate not appeared in Application_Startup

我正在 App.cs 中制作启动画面。当我使用 SplashWorker.RunWorkerAsync(); DataTemplate 时没有出现。
但是在注释这段代码的时候,出现了DataTemplate....

已正确注册 ViewModel 和 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();
    }
}

正如@AnjumSKhan 在评论中提到的,将您的 SplashWorker 代码移到应用程序的 Startup 事件中,因为 DataTemplate 会在绑定时工作,这发生在 InitializeComponent 方法之后。