WPF:窗口关闭后无法设置可见性或调用显示。试图在Splash之后打开一个主窗口

时间:2017-03-25 12:33:58

标签: c# wpf winforms

在加载所有应用程序数据后,需要在关闭启动画面后打开主窗口。

它给出了错误

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    DispatcherHelper.Initialize();

    Global = new AARFID.KeyManagement.UI.Properties.Settings();

    Splash splash = new Splash();
    var isClosed = splash.ShowDialog();
    if (isClosed == true)
    {
        splash.Close();
    }

    UI.Main m = new UI.Main();
    m.Show();
}

2 个答案:

答案 0 :(得分:0)

我更喜欢在不同的线程中打开启动画面。

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{        
    public Form1()
    {
        Thread t = new Thread(new ThreadStart(SplashStart));
        t.Start();
        Thread.Sleep(1500);
        InitializeComponent();
        t.Abort();
        Form1.Activate(); 
    }        
    public void SplashStart()
    {
        Application.Run(new SplashScreen());
    }
}
}   

这是我的工作代码。你可以尝试这个解决方案。

答案 1 :(得分:0)

我使用Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

修复此问题
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    DispatcherHelper.Initialize();

    Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

    Global = new Properties.Settings();

    Splash splash = new Splash();
    var isClosed = splash.ShowDialog();

    Current.ShutdownMode = ShutdownMode.OnMainWindowClose;

    if (isClosed == true)
    {
        var main = new UI.Main();
        this.MainWindow = main;
        this.MainWindow.Show();
    }
}