Redigera

Dela via


Get activation info for packaged apps

Starting in Windows 10, version 1809, packaged desktop apps can call the AppInstance.GetActivatedEventArgs method to retrieve certain kinds of app activation info during startup. For example, you can call this method to get info related to app activation from opening a file, clicking an interactive toast, or using a protocol. Starting in Windows 10, version 2004, this feature is also supported in packaged apps with external location (see Grant package identity by packaging with external location).

Note

In addition to retrieving certain types of activation info by using the AppInstance.GetActivatedEventArgs method as described in this article, you can also retrieve activation info for background tasks by defining a COM class. For more info, see Create and register a winmain COM background task.

Code example

The following code example demonstrates how to call the AppInstance.GetActivatedEventArgs method from the Main function in a Windows Forms app. For each activation type your app supports, cast the args return value to the corresponding event args type. In this code example, the Handlexxx methods are assumed to be dedicated activation handler code that you have defined elsewhere.

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    var args = AppInstance.GetActivatedEventArgs();
    switch (args.Kind)
    {
        case ActivationKind.Launch:
            HandleLaunch(args as LaunchActivatedEventArgs);
            break;
        case ActivationKind.ToastNotification:
            HandleToastNotification(args as ToastNotificationActivatedEventArgs);
            break;
        case ActivationKind.VoiceCommand:
            HandleVoiceCommand(args as VoiceCommandActivatedEventArgs);
            break;
        case ActivationKind.File:
            HandleFile(args as FileActivatedEventArgs);
            break;
        case ActivationKind.Protocol:
            HandleProtocol(args as ProtocolActivatedEventArgs);
            break;
        case ActivationKind.StartupTask:
            HandleStartupTask(args as StartupTaskActivatedEventArgs);
            break;
        default:
            HandleLaunch(null);
            break;
    }

Supported activation types

You can use the AppInstance.GetActivatedEventArgs method to retrieve activation info from the supported set of event args objects listed in the following table. Some of these activation types require the use of a package extension in the package manifest.

ShareTargetActivatedEventArgs activation info is supported only on Windows 10, version 2004, and later. All other activation info types are supported on Windows 10, version 1809, and later.

Event args type Package extension Related docs
ShareTargetActivatedEventArgs uap:ShareTarget Making your desktop application a share target
ProtocolActivatedEventArgs uap:Protocol Start your application by using a protocol
ToastNotificationActivatedEventArgs desktop:ToastNotificationActivation Toast notifications from desktop apps.
StartupTaskActivatedEventArgs desktop:StartupTask Start an executable file when users log into Windows
FileActivatedEventArgs uap:FileTypeAssociation Associate your packaged application with a set of file types
VoiceCommandActivatedEventArgs None Activate a foreground app with voice commands through Cortana
LaunchActivatedEventArgs None