【问题标题】:Passing command line arguments to WPF Core 3.0 sideloaded application将命令行参数传递给 WPF Core 3.0 旁加载应用程序
【发布时间】:2020-02-13 10:11:51
【问题描述】:

我有一个在 Core 3.0 上运行的 WPF 应用程序。我通过 Windows 应用程序打包项目发布了它。因为它是 WPF 应用程序,所以我需要将其添加到 appxmanifest:

<Extensions>
  <uap5:Extension
    Category="windows.appExecutionAlias"
    Executable="MyApp.exe"
    EntryPoint="MyApp">
    <uap5:AppExecutionAlias>
      <uap5:ExecutionAlias Alias="MyApp.exe" />
    </uap5:AppExecutionAlias>
  </uap5:Extension>
</Extensions>

然后我将 WPF 应用程序发布为旁加载,当我从 Windows 开始菜单启动它时它工作得很好。问题是我需要从命令行启动它,并且我需要传递一些命令行参数。但是,当我通过键入别名从命令行启动应用程序时,它只传递这个参数: -服务器名称:App.App7adfdfg54shnsdfh87asrgsdfg1.mca 这不是我的论点——我的论点根本没有通过。因此,我尝试将其添加到我的 WPF .csproj 文件中:

<Reference Include="Windows.Foundation.UniversalApiContract">
  <HintPath>C:\Program Files (x86)\Windows Kits\10\References\10.0.18362.0\Windows.Foundation.UniversalApiContract\8.0.0.0\Windows.Foundation.UniversalApiContract.winmd</HintPath>
  <IsWinMDFile>true</IsWinMDFile>
</Reference>

所以我可以在我的 WPF App.xaml.cs 文件中调用它:

using test = Windows.ApplicationModel.AppInstance;
...
Windows.ApplicationModel.Activation.IActivatedEventArgs args = test.GetActivatedEventArgs();

但是在这样做之后,args 为空。任何想法如何解决这个问题并实际将参数传递给我的 WPF 应用程序?谢谢。

【问题讨论】:

  • 您好,您不能通过命令行(甚至 WPF 应用程序)将参数传递给 UWP 应用程序。您可以考虑使用 URI 方案而不是命令行方案,这是 documentation
  • 你在哪里打电话Windows.ApplicationModel.AppInstance.GetActivatedEventArgs()?当它是 null 时,你是否在容器内运行?
  • 我在 App.xaml.cs 文件中 OnStartup 方法的开头调用它。
  • @JiříBatulka:你如何确认它是null?
  • @mm8 if (args == null) 语句后的简单消息框。

标签: c# .net wpf .net-core uwp


【解决方案1】:

我在 .appxmanifestlines 文件中遗漏了几行。 添加他的参考:

xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" 

将其添加到 IgnorableNamespaces:

IgnorableNamespaces="uap uap5 rescap desktop4">

并将其添加到 AppExecutionAlias:

<uap5:AppExecutionAlias desktop4:Subsystem="console">
     <uap5:ExecutionAlias Alias="DeploymentScripts.UI.exe" />
</uap5:AppExecutionAlias>

然后甚至可以从Environment.GetCommandLineArgs() 访问参数

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2018-11-16
    • 2010-11-13
    • 2010-09-30
    • 2011-02-16
    相关资源
    最近更新 更多