UWP: run the command line to start the UWP application and the uwp command line.

Source: Internet
Author: User
Tags creators update

UWP: run the command line to start the UWP application and the uwp command line.

Recently, during application development, I met the requirements described in the title. In fact, the main purpose is to quickly start the application, just as you can enter some executable program names in the "run" dialog box, you can start it directly. In this way, you can increase the ease of use of the App. After checking some documents, I learned that after Windows Build 16266, I added related APIs. Therefore, to implement and use this function, the Windows system and SDK versions must be later than 16266, fall Creators Update (Build 16299) fully meets this condition.

Implementation

To use the command line to start the UWP application, it is actually very simple. You only need two steps: first, in the Package. appExecutionAlias extension is added to appxmanifest. the OnActived event is processed accordingly.

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.appExecutionAlias"            Executable="TestCmdLineApp.exe"            EntryPoint="TestCmdLineApp.App">            <uap5:AppExecutionAlias>              <uap5:ExecutionAlias Alias="App.exe" />            </uap5:AppExecutionAlias>          </uap5:Extension>              </Extensions>    </Application>  </Applications>  ...</Pakage>

The bold part is what we need to add. We can see that we have added an Extension named appExecutionAlias ). There are several attributes in the Extension node. Their meanings are as follows:

1) The Category attribute specifies the Extension Category. For our current requirement, its value is fixed to windows. appExecutionAlias, which provides an alias for the running of the application;
2) the Executable attribute specifies the name of the exe of the current application, that is, the Assembly name + ". exe ";
3) The EntryPoint attribute specifies the entry point of the current application, that is, the full name of the App class (including its namespace );
4) the Alias attribute in the AppExecutionAlias \ ExecutionAlias node is the name of the command line to be defined for the current application. Three points must be noted here:

A) It can be the same or different from the previous Executable property value. For example, it can be shorter, so that users can remember and enter it;
B) if the defined alias has been occupied by other applications installed on the current machine, it will not take effect, that is, Whoever occupies it will be effective (of course, if the previously occupied application is uninstalled, this alias can be used by your application );
C) You can add multiple ExecutionAlias nodes and specify multiple aliases for the application. By providing more aliases for an application, you can solve the problem of Alias occupation (if this problem exists ).

2. Process OnActivated events

In the App. in the OnActived event, we determine the IActivatedEventArgs parameter type. If the Kind attribute is CommandLineLaunch, it is considered to be a command line startup. What we will do next is like in the OnLaunched event, initialize the Frame and navigate to the home page of the application as follows:

        protected override void OnActivated(IActivatedEventArgs args)        {            if (args.Kind == ActivationKind.CommandLineLaunch)            {                // ...            }            Frame rootFrame = Window.Current.Content as Frame;            if (rootFrame == null)            {                rootFrame = new Frame();                rootFrame.NavigationFailed += OnNavigationFailed;                Window.Current.Content = rootFrame;            }            rootFrame.Navigate(typeof(MainPage));            Window.Current.Activate();            base.OnActivated(args);        }
3. Parameter Processing

Using the command line to start an application has a great advantage. You can include parameters such as app.exe a, app.exe a B, and app.exe/type: a at startup, applications are processed based on the parameters provided by users. To get the parameters passed by the user, you only need to convert the IActivatedEventArgs type parameter to CommandLineActivatedEventArgs through its Operation. the Arguments attribute can be obtained, and the rest is to analyze the parameters and process them accordingly. In addition to parameters, we can also obtain the directory from which the App is started, which is obtained through the Operation. CurrentDirectoryPath attribute. The complete code is as follows:

        protected async override void OnActivated(IActivatedEventArgs args)        {            string arugment = string.Empty;            if (args.Kind == ActivationKind.CommandLineLaunch)            {                var cmdArgs = args as CommandLineActivatedEventArgs;                StringBuilder sb = new StringBuilder();                sb.AppendLine($"Argument: {cmdArgs.Operation.Arguments}");                sb.AppendLine($"CurrentDirectoryPath: {cmdArgs.Operation.CurrentDirectoryPath}");                await new MessageDialog(sb.ToString()).ShowAsync();            }            Frame rootFrame = Window.Current.Content as Frame;            if (rootFrame == null)            {                rootFrame = new Frame();                rootFrame.NavigationFailed += OnNavigationFailed;                Window.Current.Content = rootFrame;            }            rootFrame.Navigate(typeof(MainPage), arugment);            Window.Current.Activate();            base.OnActivated(args);        }

Finally, Deploy the application to test the effect.

After deployment, enter the alias (and parameter) defined above in the "run" (Win + R) dialog box. Of course, you can enter an alias in the "command prompt" window or even in the address bar of the "Resource Manager" window to start the application.

Principles

Why can we run an application after entering an alias in these locations? To solve this problem, we should first use the where command to see where the corresponding command is. In the "command prompt" window, enter the where alias to obtain the following result:

C:\Users\Admin>where appC:\Users\Admin\AppData\Local\Microsoft\WindowsApps\App.exe

Open the corresponding path in "Resource Manager" and you will see that all the applications using aliases in the current machine are stored in this directory. In fact, the files here can be considered as a shortcut.

In addition, this directory is also in the PATH environment variable (you can use the path command in the "command prompt" or view it in the "environment variable" dialog box of "System Properties ), therefore, we can start the application at any location.

In addition, as a user, we can also create shortcuts for applications on the desktop (or any other directory), right-click the desktop-> create shortcuts, enter the alias and parameters (optional ). Double-click the shortcut icon to start the application. This is similar to creating a tile. However, it is more flexible than a tile. You can even create multiple shortcuts for different parameters or specify different icons for each shortcut. In this way, does it feel more like a Win32 application?

Summary

This article mainly introduces how to use command line to start UWP applications. Providing this feature can increase ease of use and flexibility for applications. As App users, apps can be opened and used more conveniently and flexibly. In this way, the UWP application and Win32 program behavior are more consistent.

References:

Command-Line Activation of Universal Windows Apps

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.