It is important to note, that when OnActivated
is executed, the OnLaunched
method is not. You must make sure to initialize the application the same way as you do in the OnLaunched
method.
First - do not remove the OnLaunched
method - that will make the app impossible to debug from Visual Studio, so uncomment it.
Next, OnActivated
method needs to initialize the frame if it does not yet exist (app is not already running) and navigate to the first page. Also - use ActivationKind.CommandLineLaunch
to recognize that the app has been launched from the command line. Finally, activate the Window.Current
instance. I have downloaded your sample and tested to confirm this code works.
protected override void OnActivated(IActivatedEventArgs args)
{
var rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
//Navigate to main page
rootFrame.Navigate(typeof(MainPage));
}
//Command line activation
if (args.Kind == ActivationKind.CommandLineLaunch)
{
var commandLineArgs = args as CommandLineActivatedEventArgs;
//Read command line args, etc.
}
//Make window active (hide the splash screen)
Window.Current.Activate();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…