如何使用Windows Template Studio处理uwp app Uri

问题描述 投票:0回答:1

根据Microsoft Docs,您可以使用此代码处理app uri

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
  }
 }

但那是从默认模板创建应用程序的时候。我的UWP项目是使用Windows Template Studio创建的,目前,我想将该代码放入其中

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

导致这个......

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();
      }
 }

如果有人之前一直在使用Windows Template Studio,请。请告诉我如何在此代码中处理Uri。我不知道把它放在哪里。目前,我的应用程序是从Uri启动时启动到空白页面

c# uwp uri windows-template-studio
1个回答
0
投票

不确定你是否还在寻找这个,但我刚想通了。

按照文档中的步骤在此处为您的应用添加身份验证:https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-windows-store-dotnet-get-started-users

但有两点不同:

首先,不要在MainPage.cs上更改OnNavigatedTo方法,因为docs会指示您这样做,将其保留为默认值。

然后在您的ActivationService.cs中,修改以下内容:

        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();
        }

添加这个:

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

这是结果:

        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();
        }

我希望这有帮助。为我工作了一个款待:)

© www.soinside.com 2019 - 2024. All rights reserved.