2017-09-13 68 views
0

根據微軟的文檔,你可以處理的應用程序的URI,此代碼如何處理UWP應用程序的URI使用Windows模板工作室

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

但是,這是當應用程序是默認的模板創建。我與Windows模板Studio和目前的地方UWP項目怡創,我猜想把這些代碼在現在

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模板工作室。請告訴我如何處理此代碼中的Uri。我不知道該把它放在哪裏。目前,我的應用程序從Uri發佈時啓動到空白頁面

+0

我認爲[this](https://github.com/Microsoft/WindowsTemplateStudio/blob/master/docs/activation.md)可能會有所幫助。 – CoCaIceDew

回答

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

希望這有助於。爲我工作:)

+0

我真的結束了不得不作出新的類 ProtocalLaunchActivationHandler:ActivationHandler 但我很樂意刪除該類,並使用幾行代碼來代替。 所以謝謝:D – user3610865

相關問題