Add and Switch Splash Screens in .NET MAUI

Learn to implement a custom splash screen in .NET MAUI and switch to the main page in both normal and MVVM architectural patterns.

  1. Normal Page Mode
    In the constructor of App.xaml.cs, replace the MainPage object.

    1
    2
    3
    4
    5
    6
    
    public App()
    {
        InitializeComponent();
    
        MainPage = new NewPage1();
    }
    
  2. MVVM Mode
    Override the CreateWindow method in App.xaml.cs.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    protected override Window CreateWindow(IActivationState? activationState)
    {
        NewPage1? desktopShellService = Handler.MauiContext!.Services.GetService<NewPage1>();
        Window window = new Window(desktopShellService)
        {
            Width = DeviceDisplay.Current.MainDisplayInfo.Width / 2,
            Height = DeviceDisplay.Current.MainDisplayInfo.Height / 2,
        };
        return window;
    }
    
  3. Switch from Splash Screen to Main Page
    In the page’s Loaded event or other events, add code to switch the MainPage object.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    async Task Loaded()
    {
        await Task.Delay(5000);
        _ = Task.Run(() =>
        {
            AppShell? desktopShellService = Application.Current.Handler.MauiContext!.Services.GetService<AppShell>();
            if (desktopShellService != null && desktopShellService != Application.Current.MainPage)
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    Application.Current.MainPage = desktopShellService;
                });
            }
        });
        //AppShell? desktopShellService = Application.Current.Handler.MauiContext!.Services.GetService<AppShell>();
        //if (desktopShellService != null && desktopShellService != Application.Current.MainPage)
        //{
        //    Application.Current.MainPage = desktopShellService;
        //}
    }
    
Built with Hugo
Theme Stack designed by Jimmy