Reputation: 23868
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:
Snapshot of Splash Screen [that just stays there when running the app via command line]:
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]:
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>Assets\StoreLogo.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="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png"
Description="UWPTest"
BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" ShortName="MyTestApp" Square71x71Logo="Assets\SmallTile.png" Square310x310Logo="Assets\LargeTile.png">
<uap:ShowNameOnTiles>
<uap:ShowOn Tile="square150x150Logo"/>
<uap:ShowOn Tile="wide310x150Logo"/>
</uap:ShowNameOnTiles>
</uap:DefaultTile >
<uap:SplashScreen Image="Assets\SplashScreen.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:
Hello World
type project - nothing fancy. If you like, you can make any changes to make it work and then let me your suggestion.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
.Upvotes: 1
Views: 1611
Reputation: 39102
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();
}
Upvotes: 3
Reputation: 32785
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();
}
Upvotes: 1