简体   繁体   中英

How to handle uwp app Uri that using Windows Template Studio

According to Microsoft Docs you can handle app uri with this code

protected override void OnActivated(IActivatedEventArgs args)
{
  if (args.Kind == ActivationKind.Protocol)
  {
     ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
     // TODO: Handle URI activation
     // The received URI is eventArgs.Uri.AbsoluteUri
  }
 }

But that was when app is create from default template. My UWP project iscreate with Windows Template Studio and currently, the place that I suppose to put that code in is now

protected override async void OnActivated(IActivatedEventArgs args)
{
    await ActivationService.ActivateAsync(args);
}

Which lead to this..

public async Task ActivateAsync(object activationArgs)
{
    if (IsInteractive(activationArgs))
    {
       // Initialize things like registering background task before the app is loaded
       await InitializeAsync();
       // Do not repeat app initialization when the Window already has content,
       // just ensure that the window is active
       if (Window.Current.Content == null)
       {
           // Create a Frame to act as the navigation context and navigate to the first page
           Window.Current.Content = _shell;
           NavigationService.Frame.NavigationFailed += (sender, e) =>
           {
                throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
           };
           NavigationService.Frame.Navigated += OnFrameNavigated;
           if (SystemNavigationManager.GetForCurrentView() != null)
           {
                SystemNavigationManager.GetForCurrentView().BackRequested += OnAppViewBackButtonRequested;
           }
        }
     }
     var activationHandler = GetActivationHandlers().FirstOrDefault(h => h.CanHandle(activationArgs);
     if (activationHandler != null)
     {
         await activationHandler.HandleAsync(activationArgs);
     }
     if (IsInteractive(activationArgs))
     {
         var defaultHandler = new DefaultLaunchActivationHandler(_defaultNavItem);
         if (defaultHandler.CanHandle(activationArgs))
         {
             await defaultHandler.HandleAsync(activationArgs);
         }
         // Ensure the current window is active
         Window.Current.Activate();
         // Tasks after activation
         await StartupAsync();
      }
 }

Please, if anyone has been using Windows Template Studio before. Please tell me how to handle Uri in this code. I don't know where to put it in. Currently, my app is launch to blank page when launch from Uri

Not sure if you are still looking for this but I have just figured it out.

Follow the steps in the docs for adding authentication to your app here: https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-windows-store-dotnet-get-started-users

But with two differences:

First, do not change your OnNavigatedTo method on the MainPage.cs as docs instruct you to do so, leave it as the default.

and then in your ActivationService.cs , modify the following:

        if (IsInteractive(activationArgs))
        {
            var defaultHandler = new DefaultLaunchActivationHandler(_defaultNavItem);
            if (defaultHandler.CanHandle(activationArgs))
            {
                await defaultHandler.HandleAsync(activationArgs);
            }

            // Ensure the current window is active
            Window.Current.Activate();

            // Tasks after activation
            await StartupAsync();
        }

by adding this:

            if (((IActivatedEventArgs)activationArgs).Kind == ActivationKind.Protocol)
            {
                var protocolEventArgs = activationArgs as ProtocolActivatedEventArgs;
                App.<<YOUR_CLOUD_SERVICE_NAME_HERE>>.ResumeWithURL(protocolEventArgs.Uri);
            }

This is the result:

        if (IsInteractive(activationArgs))
        {
            var defaultHandler = new DefaultLaunchActivationHandler(_defaultNavItem);
            if (defaultHandler.CanHandle(activationArgs))
            {
                await defaultHandler.HandleAsync(activationArgs);
            }

            if (((IActivatedEventArgs)activationArgs).Kind == ActivationKind.Protocol)
            {
                var protocolEventArgs = activationArgs as ProtocolActivatedEventArgs;
                App.KPMPClient.ResumeWithURL(protocolEventArgs.Uri);
            }

            // Ensure the current window is active
            Window.Current.Activate();

            // Tasks after activation
            await StartupAsync();
        }

I hope this helps. Worked a treat for me :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM