0

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;
}
Muhammad Touseef
  • 4,357
  • 4
  • 31
  • 75

1 Answers1

3

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;
        }
    }
Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13
  • is the namespace "Windows.ApplicationModel.Activation" for uwp ? I saw it that I could access that class as u can see in commented code but I wasnt sure if that should be used in windowsAppSDK project. – Muhammad Touseef Sep 15 '22 at 09:15
  • @MuhammadTouseef Yes, this is the API from UWP. Based on the document I posted, this is the class used in the Windows APP SDK for Protocol activation. – Roy Li - MSFT Sep 15 '22 at 09:19