Passing command line arguments to WPF Core 3.0 sideloaded application

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 10:04:21

问题


I have a WPF app that runs on Core 3.0. I published it via Windows Application Packaging Project. Because it's WPF app, I needed to add this to appxmanifest:

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

Then I published the WPF app as sideloaded and it works just fine when I start it from the Windows Start menu. The problem is that I need to start it from command line and I need to pass few command line arguments. But, when I start the app from command line by typing the alias, it only passes this argument: -ServerName:App.App7adfdfg54shnsdfh87asrgsdfg1.mca which is not my argument - mine arguments are not passed at all. So then I tried to add this to my WPF .csproj file:

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

so I could call this in my WPF App.xaml.cs file:

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

But after doing this, args are null. Any ideas how to solve this and actually pass arguments to my WPF app? Thanks.


回答1:


I was missing few lines in the .appxmanifestlines file. Add he reference:

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

Add it to the IgnorableNamespaces:

IgnorableNamespaces="uap uap5 rescap desktop4">

And add this to AppExecutionAlias:

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

Then it's possible to access arguments even from Environment.GetCommandLineArgs()



来源:https://stackoverflow.com/questions/58416530/passing-command-line-arguments-to-wpf-core-3-0-sideloaded-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!