从命令行运行UWP应用程序是;只有";显示应用程序的启动屏幕
我正在使用在线教程通过从命令行运行UWP应用程序是;只有";显示应用程序的启动屏幕,uwp,windows-10,Uwp,Windows 10,我正在使用在线教程通过PowerShell在windows10-最新版本更新1903上执行名为UWPTest的UWP应用程序。应用程序成功打开,但仅显示启动屏幕。此外,除非我使用右上角的X按钮将其关闭,否则启动屏幕将永远停留在那里问题:原因可能是什么?我们如何解决 注意: 启动屏幕没有挂起,只是停留在那里,我可以移动它,最大化,最小化,等等 当我从“开始”菜单运行应用程序时,它按预期工作,并显示以下主页 参考: 启动屏幕的快照[通过命令行运行应用程序时,该快照就停留在那里]: 应用程序主窗口的
PowerShell
在windows10-最新版本更新1903
上执行名为UWPTest
的UWP
应用程序。应用程序成功打开,但仅显示启动屏幕。此外,除非我使用右上角的X
按钮将其关闭,否则启动屏幕将永远停留在那里问题:原因可能是什么?我们如何解决
注意:
VS2019
或从windows开始菜单运行应用程序,通常会显示此窗口。但现在通过命令行运行应用程序时确实会显示此窗口]:
UWPTest应用程序的My appxmanifest文件
<?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>
UWPTest
myDesktopUserName
资产\StoreLogo.png
如果你想自己测试的话。这是一个非常基本的Hello World
类型的项目-没什么特别的。如果你愿意,你可以做任何改变,让它工作,然后让我你的建议
进一步说明:从“开始”菜单或从VS2019
启动时,应用程序会成功运行。但是,正如所建议的,当我在App.xaml.cs
中添加OnActivated(…)
方法以使其从命令行运行时,它只显示启动屏幕
从命令行运行UWP应用程序“仅”显示应用程序的启动屏幕
当您使用命令行启动应用程序时,应用程序将触发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();
}
需要注意的是,当执行OnActivated
时,不会执行OnLaunched
方法。您必须确保初始化应用程序的方式与在OnLaunched
方法中相同
首先-不要删除OnLaunched
方法-这将使应用程序无法从Visual Studio进行调试,因此请取消对其注释
下一步,OnActivated
方法需要初始化帧,如果它还不存在(应用程序还没有运行),并导航到第一页。另外-使用ActivationKind.CommandLineLaunch
识别应用程序已从命令行启动。最后,激活窗口。当前实例。我已经下载了您的示例并进行了测试,以确认此代码是否有效
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();
}
在Application.xaml.cs
类中,我将OnLaunched
方法替换为建议的OnActivated
方法(使用建议的代码)但是命令行仍然只是打开启动屏幕。我在上面的帖子中添加了一个更新部分,并提供了一个指向项目副本的链接-以防您想看到我的真实代码谢谢您的帮助-特别是概述了代码背后的逻辑。。它就像一个符咒。另外,在启用了OnLaunched
方法(并为激活的添加代码)之后,应用程序现在可以在有命令行和无命令行的情况下工作(即,从开始菜单和VS2019)。