我想在我的 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();
}
在我這樣做之前,我想做兩件事(見方法第一部分的評論):
- 檢查資料庫連接是否可用。
- 登錄用戶
我想在帶有視圖模型和執行檢查的邏輯的單獨視窗中執行此操作。我確信我可以使用視圖模型及其邏輯實作視窗。
但是,在實體化“NavigationRootWindow”之前,我根本無法顯示任何型別的視窗/啟影片面。如果登錄成功,在實體化“NavigationRootWindow”之前,我需要再次關閉啟影片面/登錄視窗。據我了解,我無法實體化另一個“視窗”派生型別,因為只有一個應用程式視窗。
你能建議一種顯示啟影片面/從“OnLaunched”方法中觸發的一些模態對話框的方法嗎?此螢屏的結果將決定應用程式是否可以繼續。我也愿意接受其他建議。
謝謝你。
uj5u.com熱心網友回復:
- 創建單個視窗
- 將其
Content和DataContext設定為“閃屏” - 在顯示啟影片面時執行初始化操作
- 初始化代碼完成后,將視窗的
Content和替換為DataContext您的主要內容和視圖模型
像這樣的東西:
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!" };
}
uj5u.com熱心網友回復:
我修改/擴展了用戶 mm8 的解決方案。
在
OnLaunched我的App類的方法中,我實體化了一個LoginUserControl將臨時替換視窗內容的方法。我記得在類的一個欄位中視窗的原始內容。當該
OnLaunched方法結束時,用戶會看到帶有LoginUserControlas 內容的視窗。我的
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(); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313980.html
