Tulika
Tulika

Reputation: 685

Protocol Activation in UWP

I am working on a UWP test App, in which protocol activation needs to be added. The activation logic is written in a class library which is then added to the UWP test App as a reference. The issue is if I write the logic in OnActivated event of the test App it works fine, but when the same logic is written in a function in the class library and this function is being called from the App.xaml.cs OnActivated event, then this OnActivated event gets called in an endless loop.

Is a newframe required to be created in OnActivated event?

Upvotes: 1

Views: 792

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39092

When OnActivated is called, you have to check whether or not it was already initialized or not (because OnLaunched is not called) - create the frame and activate the Window in case it wasn't. It is usually possible to share this initialization code between OnLaunched and OnActivated events.

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

Upvotes: 7

Related Questions