简体   繁体   中英

Passing Arguments to another WPF Application doesn't work

i got a problem when passing two arguments from one WPF App to another WPF App. I publish the second WPF App to my Desktop and i want to start it with my first WPF App.

First-Program:

public MainWindow()
{
    InitializeComponent();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = @"C:\Users\User\Desktop\Work.application";
    startInfo.Arguments = "test 1234";
    Process.Start(startInfo);
}

To get the arguments in the second program i tried the following code

1.Get Arguments in Mainwindow with Environment.GetCommandLineArgs() => doesn't work

public MainWindowSecondProgram()
{
    InitializeComponent();
    string[] args = Environment.GetCommandLineArgs();
    foreach (String element in args)
    {
        MessageBox.Show(element);
    }
}

2.Get Arguments in App by using startup function => doesn't work

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        foreach (string element in e.Args)
        {
            MessageBox.Show(element);
        }
    }
}

Now if i copy the Work.exe (not Work.application) from my Visual Studio Project folder to my Desktop and change the path from

@"C:\Users\User\Desktop\Work.application" to
@"C:\Users\User\Desktop\Work.exe"

and run my first program again, it works perfect with the first function and the second function.

So why is it working with the EXE but not with the published application?

Edit: I tested both functions by passing two arguments threw the debugger and it works, but not by passing it to the published application, only EXE works.

For a Windows Store App, you need to use Application.OnLaunched . Try this code:

public partial class App : Application
{
    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {

            MessageBox.Show(args.Arguments);
    }
}

Note that you'll have to turn that string into an array yourself.

To read arguments in a ClickOnce application, use:

AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData

You can read more here .

Also, not sure, but you may need to run the ClickOnce app via the .appref-ms shortcut as opposed to the .application file itself.

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