XAML WinUI:在Application类的OnLaunched方法中显示带有启动逻辑的闪屏

hzbexzde  于 2023-03-21  发布在  其他
关注(0)|答案(2)|浏览(100)

我想在我的WinUI 3应用程序的“OnLauched”方法中添加一些闪屏(模态)窗口。
目前我只是示例化了我的主窗口,它的类型是“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.检查数据库连接是否可用。
1.登录用户
我希望在一个单独的窗口中使用视图模型和执行检查的逻辑来完成这一操作,我确信我可以使用视图模型及其逻辑来实现该窗口。
但是,在示例化“NavigationRootWindow”之前,我无法显示任何类型的窗口/启动画面。如果登录成功,我需要在示例化“NavigationRootWindow”之前再次关闭启动画面/登录窗口。据我所知,我无法示例化另一个“Window”派生类型,因为只有一个应用程序窗口。
你能建议一种方法来显示从“OnLaunched”方法中触发的启动画面/一些模态对话框吗?此屏幕的结果将决定应用程序是否可以继续。我也欢迎其他建议。
谢谢你。

pu3pd22g

pu3pd22g1#

  • 创建单个窗口
  • 将其ContentDataContext设置为“启动屏幕”
  • 在显示闪屏时执行初始化操作
  • 初始化代码完成后,用主内容和视图模型替换窗口的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!" };
}
tuwxkamq

tuwxkamq2#

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

  • 在我的App类的OnLaunched方法中,我示例化了一个LoginUserControl,它将临时替换窗口的内容,我在类的一个字段中记住了窗口的原始内容。
  • OnLaunched方法结束时,用户将看到以LoginUserControl为内容的窗口。
  • 我的LoginUserControl将通过UserLoggedIn事件或UserAbortedLogIn事件关闭。
  • 如果登录中止(不成功),我将通过关闭窗口中止应用程序启动。
  • 如果登录成功,我将向应用程序的其余部分发布一个UserLoginSuccessful事件,并将窗口内容重置为其原始内容(即用户登录后应看到的第一个内容)。
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();
 }

相关问题