WinUI:在应用程序的 OnLaunched 方法中使用启动逻辑显示 SplashScreen class

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

我想在我的 WinUI 3 应用程序的“OnLauched”方法中添加一些闪屏之王(模态)window。

目前我只是实例化我的主要 window,它是类型 'NavigationRootWindow',正如你在这里看到的:

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

在我这样做之前,我想做两件事(见方法第一部分的注释):

  1. 检查数据库连接是否可用。
  2. 登录用户

我想在一个单独的 window 中使用视图模型和执行检查的逻辑来完成。我确定我可以使用视图模型及其逻辑实现 window。

然而,在实例化 'NavigationRootWindow' 之前,我根本无法显示任何类型的 window / 初始屏幕。如果登录成功,在实例化 'NavigationRootWindow' 之前,我需要再次关闭初始屏幕/登录 window。 据我了解,我无法实例化另一个 'Window' 派生类型,因为只有一个应用程序 window.

您能否建议一种显示初始屏幕/从“OnLaunched”方法中触发的模式对话框的方法?此屏幕的结果将决定应用程序是否可以继续。 我也愿意接受其他建议。

谢谢。

  • 创建一个window
  • 将其 ContentDataContext 设置为“启动画面”
  • 在显示初始屏幕时执行初始化工作
  • 初始化代码完成后,用您的主要内容和视图模型替换 window 的 ContentDataContext

像这样:

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!" };
}

我修改/扩展了用户mm8的解决方案。

  • 在我的Appclass的OnLaunched方法中,我实例化了一个LoginUserControl,它会临时替换[=47]的内容=].我记得原来在class.

    的一个字段里有window的内容
  • OnLaunched 方法结束时,用户看到 window 内容为 LoginUserControl

  • 我的 LoginUserControl 将通过 UserLoggedIn 关闭 事件或 UserAbortedLogIn 事件。

  • 如果登录失败(不成功),我会通过关闭 window.

    来中止应用程序
  • 如果登录成功我post一个UserLoginSuccessful 事件到应用程序的其余部分并将 window 内容重置为 其原始内容(即用户登录后首先看到的内容)。

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