Search code examples
c#uwpwinui-3application-lifecyclewindows-app-sdk

WindowsAppSDK doesnt have ProtocolActivatedEventArgs


I am trying to handle protocol activation and as per docs I should handle all of that within OnLaunched method so that is what I am trying to do here, but Microsoft.Windows.AppLifecycle.ProtocolActivatedEventArgs doesnt exist.

enter image description here

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    var activatedArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
    var e = args.UWPLaunchActivatedEventArgs;
    InitializeRootFrame(e);
    if (activatedArgs.Kind is ExtendedActivationKind.Launch)
    {
        if (!e.PrelaunchActivated)
        {
            if (RootFrame.Content == null)
            {
                RootFrame.Navigate(typeof(LoginPage), e.Arguments);
            }
            Window.Current.Activate();
        }
    }
    else //Launched by some other means other than normal launching
    {
        try
        {
            if (activatedArgs.Kind is ExtendedActivationKind.Protocol && activatedArgs is Microsoft.Windows.AppLifecycle.ProtocolActivatedEventArgs eventArgs)
            {
                //var a = activatedArgs.Data as ProtocolActivatedEventArgs;
                var queryParameters = HttpUtility.ParseQueryString(activatedArgs.Data.Uri.Query);
                PocessQueryForToken(queryParameters);
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            RootFrame.Navigate(typeof(LoginPage));
            Window.Current.Activate();
            HasLaunched = true;
        }
    }
    HasLaunched = true;
}

Solution

  • There is only a AppActivationArguments Class in the Microsoft.Windows.AppLifecycle NameSpace. So the behavior you got is expected because you are looking for a class that doesn't even exist.

    Based on the document for AppActivationArguments, we could know that the activatedArgs we got contains a data object which has one of the following data types, depending on the activation type specified by the Kind property.

    • File ->IFileActivatedEventArgs
    • Protocol ->IProtocolActivatedEventArgs
    • StartupTask ->IStartupTaskActivatedEventArgs

    The IProtocolActivatedEventArgs should be the thing that we are looking for. The document here-ProtocolActivatedEventArgs Class shows that this Class comes from the Windows.ApplicationModel.Activation Namespace.

    So the code should looks like this:

        protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
        {
            var eventargs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
            if (eventargs.Kind is ExtendedActivationKind.Protocol && eventargs.Data is Windows.ApplicationModel.Activation.ProtocolActivatedEventArgs)
            {
                ProtocolActivatedEventArgs ProtocolArgs = eventargs.Data as ProtocolActivatedEventArgs;
                var uri = ProtocolArgs.Uri;
            }
        }