Automatically start the Windows ten UWP app

Source: Internet
Author: User

Original: Https://docs.microsoft.com/zh-cn/windows/uwp/xbox-apps/automate-launching-uwp-apps

Brief introduction

Developers have a number of options for automating the launch of Universal Windows Platform (UWP) apps. In this article, we'll explore ways to launch an app by using protocol activation and initiating activation.

Protocol activation allows an app to register itself as a handler based on a given protocol.

Startup activation is a normal app launch, such as booting from an app tile.

With each activation method, you can choose to use the command line or launcher application. For all activation methods, if the app is currently running, activation will show the app to the foreground (which will reactivate it) and provide the new activation parameters. This allows flexible use of the activation command to provide new messages to the app. It is important to note that you need to compile and deploy the project for the activation method to run the newly updated app.

Protocol activation

Follow these steps to set up protocol activation for your app:

    1. Open the package.appxmanifest file in Visual Studio.
    2. Select the Claims tab.
    3. In the available claims drop -down list, select Protocol, and then select Add.
    4. Under Properties, in the namefield, enter a unique name to launch the app.

    5. Save the file and deploy the project.

    6. After you deploy the project, you should set up protocol activation.
    7. Go to Control Panel \ All Control Panel items \ Default Programs, and then select Associate a file type or protocol with a specific program. Scroll to the Protocolssection to see if the protocols are listed.

Now that you have set up protocol activation, you can activate your app with a protocol using two options (command line or launcher application).

Command line

You can activate an app by using the command line, which is followed by a protocol name, a colon (":"), and any parameters that were set before the command starts. These parameters can be arbitrary strings, but in order to take advantage of the Uniform Resource Identifier (URI) feature, it is recommended that you follow the standard URI format:

Copy
scheme://username:[email protected]:port/path.extension?query#fragment

The URI object has a method that parses the URI string for this format. For more information, see Uri Class (MSDN).

Example:

Copy
>start bingnews:>start myapplication:protocol-parameter>start myapplication://single-player/level3?godmode=1&ammo=200

Protocol command line activation supports a maximum of 2038 Unicode characters on the original URI.

Launcher application

To start, create a separate application that supports the WinRT API. The following example shows the C + + code used in the launcher to start with protocol activation, where Packageuri is the URI for an application with any parameters; myapplication: myapplication:protocol activation arguments

Copy
BOOL Protocollaunchuri (platform::string^ URI) {iasyncoperation<bool>^ protocollaunchasyncop;       try {Protocollaunchasyncop = Windows::system::launcher::launchuriasync (ref new Uri (URI)); } catch (platform::exception^ e) {platform::string^ dbgstr = "Protocollaunchuri Exception thrown              : "+ e->tostring () +" \ n ";              OutputDebugString (Dbgstr->data ());       return false;       } concurrency::create_task (PROTOCOLLAUNCHASYNCOP). Wait (); if (Protocollaunchasyncop->status = = asyncstatus::completed) {bool Launchresult = Protocollaunchasy              Ncop->getresults (); platform::string^ dbgstr = "Protocollaunchuri" + URI + "completed.              Launch result "+ Launchresult +" \ n ";              OutputDebugString (Dbgstr->data ());       return launchresult; } else {platform::string^ dbgstr = "Protocollaunchuri" + URI + "failed. Status: "+ protocollaunchasyncop->status.tostring () + "ErrorCode:" + protocollaunchasyncop->errorcode.tostring () + "\ n";              OutputDebugString (Dbgstr->data ());       return false; }}

The protocol activation of the initiator application has the same parameter restrictions as the command line protocol activation. Both support up to 2038 Unicode characters on the original URI.

Start activation

You can also launch an app by using launch activation. No settings are required, but the application user model ID (AUMID) of the UWP app is required. AUMID is the package family name, followed by an exclamation point and an application ID.

The best way to obtain the package family name is to complete the following steps:

    1. Open the package.appxmanifest file.
    2. On the pack and Go tab, enter the package name.

    3. If the package family name is not listed, open PowerShell and run >get-appxpackage MyPackageName to find *packagefamilyname.

The <Applications> application ID can be found in the package.appxmanifest file (opened in XML view) under the element.

Command line

Tools for performing a UWP app launch activation are installed with the Windows SDK. The tool can be run from the command line, and it will start the applied AUMID as a parameter.

Copy
C:\Program Files (x86)\Windows Kits\10\App Certification Kit\microsoft.windows.softwarelogo.appxlauncher.exe <AUMID>

It looks as follows:

CMD command:

"C:\Program Files (x86)\Windows Kits\10\App Certification Kit\microsoft.windows.softwarelogo.appxlauncher.exe" MyPackageName_ph1m9x8skttmg!AppId

Command line arguments are not supported for this option.

C # code: Process.Start (new ProcessStartInfo @ C:\Program Files (x86) \ Windows Kits\10\app Certification kit\ Microsoft.windows.softwarelogo.appxlauncher.exe "," acad3bbc-56cd-47d1-9aff-a8ef2e7ad58f_75cr2b68sm664!           AppId "));

Launcher application

You can create a separate application that supports using COM for startup. The following example shows the C + + code used in the launcher to start by initiating activation. With this code, you can create a Applicationactivationmanager object and call the activateapplication and any parameters of the AUMID that were found before passing in. For more information about other parameters, see Iapplicationactivationmanager::activateapplication Method (MSDN).

Copy
#include <ShObjIdl.h> #include <atlbase.h>hresult launchapp (lpcwstr AUMID) {HRESULT hr = CoInitializeEx (nul     LPTR, coinit_apartmentthreaded);     if (FAILED (HR)) {wprintf (L "Launchapp%s:failed to init COM. hr = 0x%08lx \ n", AUMID, HR);            } {ccomptr<iapplicationactivationmanager> appactivationmgr = nullptr; if (SUCCEEDED (HR)) {hr = CoCreateInstance (Clsid_applicationactivationmanager, nullptr, CLSC                   Tx_local_server, Iid_ppv_args (&appactivationmgr)); if (FAILED (HR)) {wprintf (L "Launchapp%s:failed to create application Activatio N Manager.                   hr = 0X%08LX \ n ", AUMID, HR);                   }} if (SUCCEEDED (HR)) {DWORD pid = 0;                   hr = Appactivationmgr->activateapplication (AUMID, nullptr, Ao_none, &pid);       if (FAILED (HR)) {                  wprintf (L "Launchapp%s:failed to Activate App. hr = 0x%08lx \ n", AUMID, HR);     }}} couninitialize (); return HR;}

It is worth noting that, unlike the previous startup method (that is, using the command line), this method supports incoming parameters.

Accept Parameters

To accept incoming parameters when you activate a UWP app, you must add some code to the app. To determine whether protocol activation or activation is initiated, override the OnActivated event and examine the parameter type, and then get the pre-parsed value of the original string or Uri object.

This example shows how to get the original string.

Copy
void OnActivated(IActivatedEventArgs^ args){        // Check for launch activation        if (args->Kind == ActivationKind::Launch)        {            auto launchArgs = static_cast<LaunchActivatedEventArgs^>(args);    Platform::String^ argval = launchArgs->Arguments;            // Manipulate arguments …        }        // Check for protocol activation        if (args->Kind == ActivationKind::Protocol)        {            auto protocolArgs = static_cast< ProtocolActivatedEventArgs^>(args);            Platform::String^ argval = protocolArgs->Uri->ToString();            // Manipulate arguments …        }    }
Summary

In summary, you can use a variety of methods to launch a UWP app. Depending on the requirements and usage, there may be other methods that are more appropriate.

Automatically start the Windows ten UWP app

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.