简体   繁体   中英

In UWP App, read command line arguments and pass it from App.xaml.cs file to the MainPage.xaml.cs

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

in UWP, parameters are usually passed between pages through navigation.

This piece of code roughly shows the structure of the OnActivated method:

protected override void OnActivated(IActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();
        rootFrame.NavigationFailed += OnNavigationFailed;
        // Place the frame in the current Window
        Window.Current.Content = rootFrame;
    }
    string commandParam = string.Empty;
    switch (args.Kind)
    {
        case ActivationKind.CommandLineLaunch:
            //get command parameter
            break;
        default:
            //do other things...
            break;
    }
    if (rootFrame.Content == null)
    {
        rootFrame.Navigate(typeof(MainPage), commandParam);
    }
    // Ensure the current window is active
    Window.Current.Activate();
}

After getting the command line parameters, the most important thing is in this line of code:

rootFrame.Navigate(typeof(MainPage), commandParam);

Through navigation, we can pass parameters to MainPage .

After this, we need to receive the parameter in the MainPage and process it:

MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if(e.Parameter!=null && e.Parameter is string commandParam)
    {
        TestTextBlock.Text = commandParam;
    }
    base.OnNavigatedTo(e);
}

For more information about navigation, you can refer to this document

Define a variable in App.xaml.cs

sealed partial class App : Application
    {
        public string cmdLineArgs;

        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }
..........
}

Then set cmdLineArgs 's value in OnActivated event.

Then you can use cmdLineArgs in anywhere of your project like

string args = (Application.Current as App).cmdLineArgs;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM