Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
981 views
in Technique[技术] by (71.8m points)

windows 10 - Running UWP app from Command Line is "ONLY" showing the splash screen of the app

I'm using Launch UWP App Via CommandLine online tutorial to execute my UWP app named UWPTest via PowerShell on Windows 10 -latest version update 1903. The app opens successfully but only shows the splash screen. Moreover, the splash screen just stays there forever unless I close it using X button on top right corner. Question: What could be the cause and how can we resolve it?

Note:

  1. The splash screen does not hang it just stays there and I can move it around, maximize, minimize, etc.
  2. When I ran the app from start menu it works as expected and shows the following Main Page.
  3. Ref: uap5:AppExecutionAlias

Snapshot of Splash Screen [that just stays there when running the app via command line]:

enter image description here

Snapshot of Main Window of the app [that appears normally if run the app from VS2019 or from the windows Start Menu. But this window does now appear when running the app via command line]:

enter image description here

My appxmanifest File of UWPTest app:

<?xml version="1.0" encoding="utf-8"?>

<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
  xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"
  IgnorableNamespaces="uap mp uap5">

  <Identity
    Name="86754353-ac66-48f5-b6bb-fdad292dd398"
    Publisher="CN=myDesktopUserName"
    Version="1.0.0.0" />

  <mp:PhoneIdentity PhoneProductId="86754353-ac66-48f5-b6bb-fdad292dd398" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

  <Properties>
    <DisplayName>UWPTest</DisplayName>
    <PublisherDisplayName>myDesktopUserName</PublisherDisplayName>
    <Logo>AssetsStoreLogo.png</Logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="UWPTest.App">
      <uap:VisualElements
        DisplayName="UWPTest"
        Square150x150Logo="AssetsSquare150x150Logo.png"
        Square44x44Logo="AssetsSquare44x44Logo.png"
        Description="UWPTest"
        BackgroundColor="transparent">
        <uap:DefaultTile Wide310x150Logo="AssetsWide310x150Logo.png" ShortName="MyTestApp" Square71x71Logo="AssetsSmallTile.png" Square310x310Logo="AssetsLargeTile.png">
          <uap:ShowNameOnTiles>
            <uap:ShowOn Tile="square150x150Logo"/>
            <uap:ShowOn Tile="wide310x150Logo"/>
          </uap:ShowNameOnTiles>
        </uap:DefaultTile >
        <uap:SplashScreen Image="AssetsSplashScreen.png" />
      </uap:VisualElements>
      <Extensions>
        <uap5:Extension Category="windows.appExecutionAlias" Executable="UWPTest.exe" EntryPoint="UWPTest.App">
          <uap5:AppExecutionAlias>
            <uap5:ExecutionAlias Alias="UWPTest.exe"/>
          </uap5:AppExecutionAlias>
        </uap5:Extension>
      </Extensions>
    </Application>
  </Applications>

  <Capabilities>
    <Capability Name="internetClient" />
  </Capabilities>
</Package>

UPDATE:

  1. A copy of the project is here if you want to test it on your own. It's a very basic Hello World type project - nothing fancy. If you like, you can make any changes to make it work and then let me your suggestion.
  2. to clarify further: The app runs successfully when launching from start menu or from VS2019. But, as suggested here, when I add OnActivated(…) method in App.xaml.cs to make it run from command line, it shows only Splash Screen.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...