如何实现UWP应用登录Windows后启动并最大化窗口?
Hey there! I’ve dealt with this exact headache before—you set up your UWP app to launch on Windows login, but it immediately tucks itself into the taskbar. Let’s go through the practical fixes that got my app launching maximized every time:
1. Handle Startup Activation in App.xaml.cs
The key here is detecting when the app is launched via your StartupTask and forcing the window state before and after activating the window. Here’s how to update your OnLaunched method:
protected override void OnLaunched(LaunchActivatedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; Window.Current.Content = rootFrame; } // Check if this launch originates from your Startup Task bool isStartupLaunch = e.PreviousExecutionState == ApplicationExecutionState.NotRunning && e.Kind == ActivationKind.Launch && !string.IsNullOrEmpty(e.Arguments) && e.Arguments.Contains("StartupTask"); // Match your task's argument if set if (isStartupLaunch) { // Option 1: Maximize to full screen (covers entire display) ApplicationView.GetForCurrentView().TryEnterFullScreenMode(); // OR Option 2: Set to standard maximized window state (not full screen) // ApplicationView.GetForCurrentView().WindowingEnvironment.Window.WindowState = WindowState.Maximized; // OR Option 3: Force launch size to match screen bounds var displayInfo = DisplayInformation.GetForCurrentView(); double screenWidth = displayInfo.ScreenWidthInRawPixels / displayInfo.RawPixelsPerViewPixel; double screenHeight = displayInfo.ScreenHeightInRawPixels / displayInfo.RawPixelsPerViewPixel; ApplicationView.PreferredLaunchViewSize = new Size(screenWidth, screenHeight); ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; } if (e.PrelaunchActivated == false) { if (rootFrame.Content == null) { rootFrame.Navigate(typeof(MainPage), e.Arguments); } // Critical: Ensure the window is activated (prevents minimizing) Window.Current.Activate(); // Double down on activation in case the system tries to hide it CoreApplication.MainView.Activate(); } }
2. Add a Safety Check in Your Main Page
Sometimes the window state doesn’t stick during launch—adding a check in your main page’s Loaded event ensures it corrects itself after the page loads:
private void MainPage_Loaded(object sender, RoutedEventArgs e) { var app = App.Current as App; if (app != null && app.IsStartupLaunch) // Add a boolean flag in App.xaml.cs to track startup launches { var appView = ApplicationView.GetForCurrentView(); appView.WindowingEnvironment.Window.WindowState = WindowState.Maximized; CoreApplication.MainView.Activate(); } }
Don’t forget to add the IsStartupLaunch flag in your App class:
public bool IsStartupLaunch { get; private set; } // Update OnLaunched to set this flag if (isStartupLaunch) { IsStartupLaunch = true; // ... rest of your window state code }
3. Verify Your Startup Task Manifest Configuration
Double-check that your Package.appxmanifest has the correct StartupTask extension (no typos here can save you hours):
<Extensions> <uap5:Extension Category="windows.startupTask" Executable="YourAppName.exe" EntryPoint="YourAppName.App"> <uap5:StartupTask TaskId="YourUniqueTaskId" Enabled="true" DisplayName="Your App Display Name" /> </uap5:Extension> </Extensions>
Important Testing Note
Don’t test this by restarting your app—you need to log out and log back in to trigger the StartupTask properly. Restarting the app manually won’t replicate the login launch behavior.
内容的提问来源于stack exchange,提问作者Viktor Tolkachev

