Search code examples
c#.netwindows-runtimewindows-store-appswin-universal-app

Why does OnLaunched check if (rootFrame.Content == null)?


In the OnLaunched method that's in the App class, there is the following:

if (rootFrame.Content == null)
{
    rootFrame.Navigate(typeof(MainPage), e.Arguments);
}

Why does it check if the Content is null? Obviously it will be, since the OnLaunched method is

"Invoked when the application is launched normally by the end user. Other entry points will be used such as when the application is launched to open a specific file." (from its summary.)

The reason given in the comment to the null check is:

"When the navigation stack isn't restored navigate to the first page,..."

But it will never be "restored" as above. This would make sense in a Resuming event handler, not here.


Solution

  • OnLaunched will be invoked any time the user opens an app through the Start menu, a tile, or any other action that normally launches an app, even if the app is already running or was previously suspended rather than terminated.

    Note that in the WinRT 8.1 templates, state is restored in the if (rootFrame == null) check that appears before this check, within the OnLaunched handler.