将命令行参数传递给WPF Core 3.0侧面加载的应用程序

时间:2019-10-16 15:10:31

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

我有一个在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“开始”菜单启动它时,它的运行情况还不错。问题是我需要从命令行启动它,并且需要传递几个命令行参数。但是,当我通过输入别名从命令行启动应用程序时,它仅传递以下参数: -ServerName: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应用程序吗?谢谢。

1 个答案:

答案 0 :(得分: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()

访问参数