UWP: Enable the UWP application to start itself, and uwp to start the application.
In the previous article, we implemented the use of command line to start UWP applications. In this article, we will implement the self-enable implementation of UWP applications, that is, after the application is started or after the user logs in, the application is started on its own. These features are originally available in Win32 programs, and UWP can support these features to further share the same behavior with Win32 programs.
Implementation
As with the implementation of command line startup, the implementation of self-startup is also divided into two steps: first, in the Package. add windows to appxmanifest. startupTask Extension (Extension); then, the OnActivated event is processed in the App class. In fact, in addition to these two steps, we also need to add the check StartupTask status and allow the user to control the self-starting logic.
1. Modify Package. appxmanifest
Right-click the Package. appxmanifest file in the project and select "open mode"-> "XML text editor" from the shortcut menu. After it is enabled, modify its content as follows:
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" IgnorableNamespaces="uap mp uap5"> ... <Applications> <Application ... <Extensions> <uap5:Extension Category="windows.startupTask" EntryPoint="AppAutoRun.App" Executable="AppAutoRun.exe"> <uap5:StartupTask DisplayName="AppAutoRun" Enabled="true" TaskId="AppAutoRun"/> </uap5:Extension> </Extensions> </Application> </Applications></Package>
The bold section above is the extended windows. startupTask. Its EntryPoint and Executable attributes indicate the complete name of the App class and the exe name of the current application respectively.
In the Extension node, a node StartupTask is added, which has three attributes:
- TaskId: task Id, required. It must be unique in all UWP applications and cannot be the same as the TaskId of other applications;
- Enabled: Enabled or not. required. Indicates whether to enable the current application as a self-starting behavior;
- DisplayName: Display name. (optional) display name in the "Start" tab of the "Task Manager;
It should be noted that the Enabled attribute should be set to false; in fact, this attribute will be ignored; Because UWP must be started at least once and request the user's consent. Currently, only one StartupTask node can be added.
In this case, Deploy the application to the local machine. Then, on the "Start" tab in "Task Manager", we can see it.
Right-click a task to control its status (enable/disable it). You can see that the current application status is "disabled ". Note: Before setting its status, the App must be started at least once. Otherwise, the settings here do not work.
2. View and change the task status
In addition to adding extensions to Package. appxmanifest, we also need to use relevant APIs to view the status of the added StartupTask and change it. Add the following code to MainPage. xaml:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Margin="12"> <StackPanel> <TextBlock x:Name="tbState" /> <Button x:Name="btnSetState" Margin="0,4,0,0" Click="btnSetState_Click" /> </StackPanel> </Grid> </Grid>
Add the following code to MainPage. xaml. cs:
Private async void MainPage_Loaded (object sender, RoutedEventArgs e) {await LoadState ();} public async Task LoadState () {var task = await StartupTask. getAsync ("AppAutoRun"); this. tbState. text = $ "Status: {task. state} "; switch (task. state) {case StartupTaskState. disabled: // Disabled this. btnSetState. content = "enable"; this. btnSetState. isEnabled = true; break; case StartupTaskState. disabledByPolicy: // this is disabled by the administrator or group policy. btnSetState. content = "disabled by system policy"; this. btnSetState. isEnabled = false; break; case StartupTaskState. disabledByUser: // this is manually disabled by the user. btnSetState. content = "disabled by users"; this. btnSetState. isEnabled = false; break; case StartupTaskState. enabled: // The current status is Enabled this. btnSetState. content = "enabled"; this. btnSetState. isEnabled = false; break;} private async void btnSetState_Click (object sender, RoutedEventArgs e) {var task = await StartupTask. getAsync ("AppAutoRun"); if (task. state = StartupTaskState. disabled) {await task. requestEnableAsync ();} // reload the await LoadState ();}
We use GetAsync of the StartupTask class (located in the Windows. ApplicationModel namespace) to obtain the self-starting task (StartupTask) with the specified TaskId ). The StartupTask class has an enumeration attribute of State to indicate its State. Their values and meanings are described in the annotations.
Note the following points:
You can use the RequestEnableAsync method of the StartupTask class to initiate a request to the user. After this method is called, the following window is displayed:
After the user selects "enable", it will automatically start after the system starts. Otherwise, if "Disable" is selected, the status will be DisabledByUser. To enable it, Open Task Manager, right-click it on the launch tab, and select enable ".
3. Process OnActivated events
Then, you can add a judgment on the ActivationKind class on the OnActivated event of the App class and perform corresponding processing. The Code is 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; } if (args.Kind == ActivationKind.StartupTask) { var startupArgs = args as StartupTaskActivatedEventArgs; } rootFrame.Navigate(typeof(MainPage), args.Kind); Window.Current.Activate(); }
Finally, note that if auto-start is enabled, the application will be started in a minimal manner after the system starts.