0
votes

When the application open, splash screen with the loading message will show, then the login window will popup in front of the splash screen. My problem now is how can I continue with when logged in successfully, the login window will disappear and the splash screen will still be displayed with different loading message and after that the splash screen will totally disappear, and finally the main window of the application will be executed? Can the startup events be updated?

Here's my code in App.xaml.cs:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        SplashScreen splashScreen = new SplashScreen();
        splashScreen.Show();

        var startupTask = new Task(() =>
            {
                //Set custom message on screen
                splashScreen.Dispatcher.BeginInvoke((Action)(() => splashScreen.LoadingMsg = "Please wait..."));
                Thread.Sleep(500);
                splashScreen.Dispatcher.BeginInvoke((Action)(() => splashScreen.LoadingMsg = "Please provide login information..."));
                Thread.Sleep(300);
            });

        //When loading finished, show login window
        startupTask.ContinueWith(t =>
            {
                Login loginWindow = new Login();

                //When login window is loaded, the splash screen stays behind the login window 
                loginWindow.Loaded += (sender, args) => splashScreen.InvalidateVisual();

                this.MainWindow = loginWindow;

                //Show the login window in modal mode
                loginWindow.ShowDialog();
            }, TaskScheduler.FromCurrentSynchronizationContext());

        startupTask.Start();            
    }
}
1
Could you combine your login screen with the splash screen? - Lee Willis

1 Answers

0
votes

Hmm, I would be worried about using Task Continuation to orchestrate the view events.

how about a mediator pattern which you can then use to get the ViewModels for your various screens to respond to events raised by the other ViewModels? eg:

using System;
using System.Collections.Generic;

namespace UnitTestProject2
{

    public class ViewModel1
    {
        IMediator mediator;
        public ViewModel1(IMediator mediator)
        {
            this.mediator = mediator;
        }

        public string UserId { get; set; }
        public void Login(string userid)
        {
            this.UserId = userid;
            this.mediator.RaiseEvent("LoggedIn", this.UserId);
        }
    }

    public class VieowModel2
    {
        IMediator mediator;
        public VieowModel2(IMediator mediator)
        {
            this.mediator = mediator;
            this.mediator.ListenFor("LoggedIn", LoggedIn);
        }
        protected string UserId;
        protected void LoggedIn(Object sender, EventArgs e)
        {
            UserId = sender.ToString();
        }
    }

    public interface IMediator
    {
        void ListenFor(string eventName, EventHandler action);
        void RaiseEvent(string eventName, object data);
    }
}