Home > OS >  WinUI: Diplays SplashScreen with startup logic in OnLaunched method of the Application class
WinUI: Diplays SplashScreen with startup logic in OnLaunched method of the Application class

Time:10-05

I would like to add some king of splash screen (modal) window in the "OnLauched" method of my WinUI 3 application.

Currently I just instanciate my main window, which is of type 'NavigationRootWindow', as you can see here:

    protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
    {
        // Display splash screen with database check and user login
        // If all is well: Proceed normally
        // If database not available or login failed: Abort with application start / close application


        // Display NavigationRootWindow (main window of the application)
        NavigationRootWindow navigationRootWindow = new NavigationRootWindow();
        m_window = navigationRootWindow;
        m_window.Activate();
    }

Before I do that, I would like to do two things (see comments in the first part of the method):

  1. Check if the database connection is available.
  2. Login the user

This I would like to do in a separate window with a view model and the logic that performs the checks. I am sure I can implement the window with the view model and its logic.

However I am simply not able to display any kind of window / splash screen before I instanciate the 'NavigationRootWindow'. If the login is sucessful, I would need to close the splash screen / login window again, before I instanciate the 'NavigationRootWindow'. As I understand, I cannot instanciate another 'Window' derived type, because there is only one application window.

Can you suggest an approach to display a splash screen / some modal dialog triggered from within the "OnLaunched" method? The result of this screen shall determine if the application can continue. I am also open for other suggestions.

Thank you.

CodePudding user response:

  • Create a single window
  • Set its Content and DataContext to the "splah screen"
  • Do your initialization stuff while the splash screen is displayed
  • Replace the Content and DataContext of the window with your main content and view model once the initialization code has completed

Something like this:

protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{

    Window m_window = new NavigationRootWindow();
    m_window.Content = new TextBlock() { Text = "Loading..." };
    m_window.Activate();

    //TODO: login the user...
    await Task.Delay(5000);

    m_window.Content = new TextBlock() { Text = "Welcome!" };
}

CodePudding user response:

I modified / extended user mm8's solution.

  • In the OnLaunched method of my App class, I instanciate a LoginUserControl which will temporarily replace the content of the window. I remember the original content of the window in a field of the class.

  • When the OnLaunched method has ended, the user sees the window with the LoginUserControl as content.

  • My LoginUserControl will be closed either with a UserLoggedIn event or a UserAbortedLogIn event.

  • In case of an aborted (unsuccessful) login I abort the application start by closing the window.

  • If the login is successful I post a UserLoginSuccessful event to the rest of the application and reset the window content to its original content (i.e. the first content the user shall see after logging in).

     protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
     {
         // Create NavigationRootWindow (main window of the application)
         NavigationRootWindow = new NavigationRootWindow();
    
         // Store current content of the window (will be used later after the login)
         _navigationRootWindowContent = NavigationRootWindow.Content;
    
         // Create LoginUserControl and attach event handlers
         LoginUserControl loginUserControl = new LoginUserControl();
         loginUserControl.UserLoggedIn  = LoginUserControl_UserLoggedIn;
         loginUserControl.UserAbortedLogIn  = LoginUserControl_UserAbortedLogIn;
    
         // Set LoginUserControl as content of the window and display the window.
         // In case of a successful login, the content will be replaced later by the original content of the window.
         NavigationRootWindow.Content = loginUserControl;
         NavigationRootWindow.Activate();
     }
    
     /// <summary>
     /// Login was aborted.
     /// </summary>
     private void LoginUserControl_UserAbortedLogIn(object sender, EventArgs e)
     {
         NavigationRootWindow.Close();
     }
    
     /// <summary>
     /// Login was successful.
     /// </summary>
     private void LoginUserControl_UserLoggedIn(object sender, UserLoggedInEventArgs e)
     {
         EventAggregator.GetEvent<UserLoginSuccessful>().Publish(e.LoginSuccessfulResult);
    
         // Restore window content to original content
         NavigationRootWindow.Content = _navigationRootWindowContent;
         NavigationRootWindow.Init();
     }
    
  • Related