我正在使用Launch UWP App Via CommandLine在线教程通过UWP
上的UWPTest
执行名为PowerShell
的{{1}}应用。该应用程序成功打开,但仅显示启动屏幕。而且,除非我使用右上角的Windows 10 -latest version update 1903
按钮将其关闭,否则初始屏幕会一直停留在那儿。 问题:可能是什么原因,我们如何解决?
注意:
启动画面快照 [仅在通过命令行运行应用程序时停留在该位置]:
应用程序主窗口的快照 [如果从X
或Windows“开始”菜单中运行该应用程序,通常会显示该快照。但是通过命令行运行应用程序时,此窗口现在会出现]:
我的UWPTest应用的appxmanifest文件:
VS2019
更新:
<?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>
类型的项目-没什么花哨的。如果您愿意,可以进行任何更改以使其正常运行,然后让我提出您的建议。Hello World
启动时,该应用程序成功运行。但是,正如建议的here一样,当我在VS2019
中添加OnActivated(…)
方法以使其从命令行运行时,它仅显示App.xaml.cs
。答案 0 :(得分:1)
请注意,执行OnActivated
时不会执行OnLaunched
方法。您必须确保以与OnLaunched
方法相同的方式初始化应用程序。
首先-不要删除OnLaunched
方法-这将使应用程序无法从Visual Studio进行调试,因此请取消注释。
接下来,OnActivated
方法需要初始化框架(如果框架不存在(应用程序尚未运行))并导航到第一页。另外-使用ActivationKind.CommandLineLaunch
识别该应用程序已从命令行启动。最后,激活Window.Current
实例。我已经下载了您的示例并进行了测试,以确认此代码有效。
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();
}
答案 1 :(得分:0)
从命令行运行的UWP应用程序为“仅”,显示应用程序的初始屏幕
使用命令行启动应用程序时,该应用程序将触发OnActivated
方法,您需要在OnActivated覆盖函数中调用Window.Current.Activate();
方法,并根据该参数导航特定页面。请使用以下内容替换您的内容。
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();
}