You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现UWP应用登录Windows后启动并最大化窗口?

Fixing UWP App Auto-Minimizing After Startup Task Launch

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 10:32:55