[英]Passing command line arguments to WPF Core 3.0 sideloaded application
我有一个在 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 开始菜单启动它时它工作得很好。 问题是我需要从命令行启动它,我需要通过几个命令行 arguments。 但是,当我通过键入别名从命令行启动应用程序时,它只传递这个参数:-ServerName:App.App7adfdfg54shnsdfh87asrgsdfg1.mca 这不是我的参数 - 我的 arguments 根本没有传递。 然后我尝试将其添加到我的 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 是 null。 任何想法如何解决这个问题并实际将 arguments 传递给我的 WPF 应用程序? 谢谢。
您应该能够从打包的 WPF 应用程序的Main
方法调用Windows.ApplicationModel.AppInstance.GetActivatedEventArgs()
,前提是您的目标是 ZAEA23489CE3AA9B6406EBB28E0CDA4301771 或更高版本。 请参阅此答案以获取示例。
如果您尝试对此进行调试,请确保您启动并调试Windows Application Packaging Project
,而不是WPF Application
项目。
我在 .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()
访问 arguments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.