本文介绍了从命令行运行 UWP 应用程序是“仅"显示应用程序的启动画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

应用程序主窗口的快照 [如果从 VS2019 或 Windows 开始菜单运行应用程序,则正常显示.但是现在通过命令行运行应用程序时会出现这个窗口]:

我的 UWPTest 应用的 appxmanifest 文件:

<mp:PhoneIdentity PhoneProductId="86754353-ac66-48f5-b6bb-fdad292dd398" PhonePublisherId="00000000-0000-0000-0000-000000000000"/><属性><DisplayName>UWPTest</DisplayName><PublisherDisplayName>myDesktopUserName</PublisherDisplayName><Logo>Assets\StoreLogo.png</Logo></属性><依赖项><TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0"/></依赖项><资源><Resource Language="x-generate"/></资源><应用程序><应用程序 ID="应用程序"可执行文件="$targetnametoken$.exe"EntryPoint="UWPTest.App"><uap:视觉元素显示名称=UWPTest"Square150x150Logo="资产\Square150x150Logo.png"Square44x44Logo="资产\Square44x44Logo.png"说明="UWPTest"背景颜色=透明"><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><uap5:Extension Category="windows.appExecutionAlias" Executable="UWPTest.exe" EntryPoint="UWPTest.App"><uap5:AppExecutionAlias><uap5:ExecutionAlias Alias="UWPTest.exe"/></uap5:AppExecutionAlias></uap5:扩展></扩展></应用程序></应用程序><能力><能力名称="internetClient"/></能力></包>

更新:

  1. 项目的副本在这里,如果您想自己测试它.这是一个非常基本的 Hello World 类型的项目 - 没什么特别的.如果您愿意,可以进行任何更改以使其正常工作,然后让我提出您的建议.
  2. 进一步澄清:从开始菜单或从 VS2019 启动时,应用程序运行成功.但是,正如建议的 here,当我在 App.xaml.cs 中添加 OnActivated(…) 方法以使其从命令行运行时,它只显示 Splash屏幕.
解决方案

需要注意的是,当 OnActivated 被执行时,OnLaunched 方法不是.您必须确保以与在 OnLaunched 方法中相同的方式初始化应用程序.

首先 - 不要删除 OnLaunched 方法 - 这将使应用无法从 Visual Studio 进行调试,因此取消注释.

接下来,OnActivated 方法需要初始化框架,如果框架尚不存在(应用程序尚未运行)并导航到第一页.此外 - 使用 ActivationKind.CommandLineLaunch 识别应用程序已从命令行启动.最后,激活 Window.Current 实例.我已经下载了您的示例并进行了测试以确认此代码有效.

protected override void OnActivated(IActivatedEventArgs args){var rootFrame = Window.Current.Content 作为框架;if (rootFrame == null){//创建一个 Frame 作为导航上下文并导航到第一页根框架 = 新框架();rootFrame.NavigationFailed += OnNavigationFailed;if (args.PreviousExecutionState == ApplicationExecutionState.Terminated){//TODO: 从先前挂起的应用程序加载状态}//将框架放置在当前窗口中Window.Current.Content = rootFrame;//导航到主页面rootFrame.Navigate(typeof(MainPage));}//命令行激活如果(args.Kind == ActivationKind.CommandLineLaunch){var commandLineArgs = args as CommandLineActivatedEventArgs;//读取命令行参数等}//使窗口处于活动状态(隐藏启动画面)Window.Current.Activate();}

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]:

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:

<?

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.
解决方案

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

这篇关于从命令行运行 UWP 应用程序是“仅"显示应用程序的启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:37