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

前端 未结 2 1768
眼角桃花
眼角桃花 2021-01-16 15:33

I\'m using Launch UWP App Via CommandLine online tutorial to execute my UWP app named UWPTest via PowerShell on Windows 10 -late

相关标签:
2条回答
  • 2021-01-16 15:54

    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();
    }
    
    0 讨论(0)
  • 2021-01-16 15:58

    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();
    }
    
    0 讨论(0)
提交回复
热议问题