UWP_Can not start app using StartupTask Class?

2019-07-14 06:23发布

问题:

I follow this article to startup my UWP app when system reboot.

https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.startuptask#uwp-app-startup-task-extension

UPDATE
I also tried that article, used exactly source code in that sample Configure your app to start at log-in
and below is phenomenon:
- when I click Request to Enable StartUp button on main page of UWP app, it shows Startup State as Enabled.
- I check Task manager, this app is enabled in startup list.
- After I restart PC and login, this app start and minimize to Taskbar immediately.
- When I click on the app icon on Taskbar, the app displays only splash screen.
- I leave the app that way for few minutes and it suddenly close without any notifications.
I can register my app to Startup list but after I login, my app does not auto start as my intention. It is always like following picture
Anyone has same problem? I really need some help. Thanks.

回答1:

If your app is stuck at the splash this typically means that your code didn't call Window.Activate().

Make sure you implement OnActivate() for ActivationKind.StartupTask as follows:

protected override void OnActivated(IActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame == null)
    {
        rootFrame = new Frame();
        Window.Current.Content = rootFrame;
    }

    string payload = string.Empty;
    if (args.Kind == ActivationKind.StartupTask)
    { 
        var startupArgs = args as StartupTaskActivatedEventArgs;
        payload = ActivationKind.StartupTask.ToString();
    }

    rootFrame.Navigate(typeof(MainPage), payload);
    Window.Current.Activate();
}


回答2:

From official document, the UWP App startup task extension is

<Package xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" ...>
...
<Applications>
    <Application ...>
        ...
        <Extensions>
          <uap5:Extension Category="windows.startupTask">
            <uap5:StartupTask
              TaskId="MyStartupId"
              Enabled="false"
              DisplayName="Test startup" />
          </uap5:Extension>
      </Extensions>
    </Application>
</Applications>

You could copy to your app's Package.appxmanifest directly. And it works in my side. Note that when your app starts at startup, it will start minimized in the taskbar.

If you use startup task extension that in Configure your app to start at log-in blog, you need to modify Executable and EntryPoint property to be equal to Application's EntryPoint property. Note, avoid using $targetnametoken$.exe wildcards in Extension.

<Extensions>
  <uap5:Extension
    Category="windows.startupTask"
    Executable="StartUpTest.exe"
    EntryPoint="StartUpTest.App">
    <uap5:StartupTask
      TaskId="MyStartupId"
      Enabled="false"
      DisplayName="Test startup" />
  </uap5:Extension>
</Extensions>