【问题标题】:WPF: Cannot set Visibility or call Show, after a Window has closed. Trying to open a main window after SplashWPF:在窗口关闭后无法设置可见性或调用显示。在 Splash 之后尝试打开主窗口
【发布时间】:2017-03-25 12:33:58
【问题描述】:

在加载所有应用程序数据后关闭闪屏后需要打开一个主窗口。

报错

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();
}

【问题讨论】:

    标签: c# wpf winforms


    【解决方案1】:

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

    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());
        }
    }
    }   
    

    它对我来说是一个工作代码。你可以试试这个作为解决方案。

    【讨论】:

      【解决方案2】:

      我使用 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();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-14
        • 2018-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多