I can start my UWP app from windows command prompt or PowerShell. I've gotten some idea on how to read the arguments passed to the UWP app, as well. But, I have not been able to find any documentation on how to pass those arguments from App.xaml.cs file to the MainPage.xaml.cs.
For example, you can define an app execution alias - as shown below - for your UWP app, so you can launch it easily from cmd or powershell:
<Extensions>
<uap5:Extension
Category="windows.appExecutionAlias"
StartPage="index.html">
<uap5:AppExecutionAlias>
<uap5:ExecutionAlias Alias="MyApp.exe" />
</uap5:AppExecutionAlias>
</uap5:Extension>
</Extensions>
You can then read arguments from the OnActivated event as shown below:
async protected override void OnActivated(IActivatedEventArgs args)
{
switch (args.Kind)
{
case ActivationKind.CommandLineLaunch:
CommandLineActivatedEventArgs cmdLineArgs =
args as CommandLineActivatedEventArgs;
CommandLineActivationOperation operation = cmdLineArgs.Operation;
string cmdLineString = operation.Arguments;
string activationPath = operation.CurrentDirectoryPath;
……..
}
Question: From the above event in App.xaml.cs file, how do I pass the value of the string cmdLineString to MainPage.xaml.cs file? Example: I pass Hello World to command line. The App.xaml.cs file reads that argument. Now through my code, I want to pass that Hello World value to MainPage.xaml.cs file so I can assign it to, say, TextBlock.Text property in the main window.
Environment: VS2019 - ver 16.5.5, Windows 10 Pro - 1903
Define a variable in
App.xaml.csThen set
cmdLineArgs's value inOnActivatedevent.Then you can use
cmdLineArgsin anywhere of your project like