UWP 中的协议激活

Tul*_*ika 0 uwp

我正在开发一个 UWP 测试应用程序,其中需要添加协议激活。激活逻辑写在一个类库中,然后添加到 UWP 测试 App 作为参考。问题是如果我在测试应用程序的 OnActivated 事件中编写逻辑它工作正常,但是当在类库中的函数中编写相同的逻辑并且从 App.xaml.cs OnActivated 事件调用此函数时,然后这个 OnActivated 事件在无限循环中被调用。

是否需要在 OnActivated 事件中创建新框架?

Mar*_*und 5

当 OnActivated 被调用时,你必须检查它是否已经被初始化(因为OnLaunched没有被调用) - 创建框架并激活窗口,以防它没有被初始化。通常可以在OnLaunchedOnActivated事件之间共享此初始化代码。

protected override void OnActivated(IActivatedEventArgs e)
{
   Frame rootFrame = Window.Current.Content as Frame;

   // Do not repeat app initialization when the Window already has content
   if (rootFrame == null)
   {
       // Create a Frame to act as the navigation context
       rootFrame = new Frame();

       rootFrame.NavigationFailed += OnNavigationFailed;

       if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
       {
          //TODO: Load state from previously suspended application
       }

       // Place the frame in the current Window
       Window.Current.Content = rootFrame;
   }

   //
   // Handle protocol activation here
   //

   // Ensure the current window is active
   Window.Current.Activate();
}
Run Code Online (Sandbox Code Playgroud)