Running UWP app from Command Line is “ONLY” showing the splash screen of the app

99封情书 提交于 2019-12-01 14:32:56

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

Running UWP app from Command Line is “ONLY” showing the splash screen of the app

The app will trigger OnActivated method when you launch app with command line, you need to invoke Window.Current.Activate(); method in OnActivated override function and navigate the specific page base on the parameter. Please use the following to replace yours.

protected override void OnActivated(IActivatedEventArgs args)
{

    if (args.Kind == ActivationKind.Protocol)
    {
        ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
        // Navigate to a view 
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();
            Window.Current.Content = rootFrame;
        }
        // assuming you wanna go to MainPage when activated via protocol
        rootFrame.Navigate(typeof(MainPage), eventArgs);

    }
    Window.Current.Activate();
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!