Making a splash screen

前端 未结 6 1217
无人及你
无人及你 2021-01-06 06:29

The concept of a splash screen doesn\'t strike me as something that should be so complicated, but I\'m having trouble getting the whole splash screen painted.

Let\'s

6条回答
  •  醉酒成梦
    2021-01-06 06:55

    Why not when you run the app open a form which loads whatever you need to load into a class, then when it's done loading, open your main form and send the class into it? Alternatively you can use a singleton to load everything.

    In your Program.cs:

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new SplashScreen());
        }
    

    Then in SplashScreen:

        public SplashScreen()
        {
            InitializeComponent();
            LoadEverything();
            this.Visible = false;
            MainForm mainForm = new MainForm(LoadedClass);
            mainForm.ShowDialog();
            this.Close();
        }
    

    I need to update this:

    Here's working code (the above is just the concept).

        public SplashScreen()
        {
            InitializeComponent();
            _currentDispatcher = Dispatcher.CurrentDispatcher;
    
            // This is just for the example - start a background method here to call
            // the LoadMainForm rather than the timer elapsed
            System.Timers.Timer loadTimer = new System.Timers.Timer(2000);
            loadTimer.Elapsed += LoadTimerElapsed;
            loadTimer.Start();
        }
    
        public void LoadMainForm()
        {
            // Do your loading here
            MainForm mainForm = new MainForm();
    
            Visible = false;
            mainForm.ShowDialog();
            System.Timers.Timer closeTimer = new System.Timers.Timer(200);
            closeTimer.Elapsed += CloseTimerElapsed;
    
            closeTimer.Start();
        }
    
        private Dispatcher _currentDispatcher;
    
        private void CloseTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (sender is System.Timers.Timer && sender != null)
            {
                (sender as System.Timers.Timer).Stop();
                (sender as System.Timers.Timer).Dispose();
            }
            _currentDispatcher.BeginInvoke(new Action(() => Close()));
        }
    
        private void LoadTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (sender is System.Timers.Timer && sender != null)
            {
                (sender as System.Timers.Timer).Stop();
                (sender as System.Timers.Timer).Dispose();
            }
            _currentDispatcher.BeginInvoke(new Action(() => LoadMainForm()));
        }
    

提交回复
热议问题