简体   繁体   English

将参数传递给另一个WPF应用程序不起作用

[英]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. 从一个WPF应用程序向另一个WPF应用程序传递两个参数时出现问题。 I publish the second WPF App to my Desktop and i want to start it with my first WPF App. 我将第二个WPF应用程序发布到我的桌面上,我想用我的第一个WPF应用程序启动它。

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 1,使用Environment在Mainwindow中获取参数.GetCommandLineArgs()=>不起作用

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 2.使用启动功能在App中获取参数=>不起作用

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 现在,如果我将Visual Studio Project文件夹中的Work.exe(不是Work.application)复制到我的桌面,并从

@"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? 那么,为什么它可以与EXE一起使用,但不能与已发布的应用程序一起使用呢?

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. 编辑:我通过传递两个参数引发调试器来测试这两个函数,并且它可以工作,但是没有通过将其传递给已发布的应用程序,只有EXE可以工作。

For a Windows Store App, you need to use Application.OnLaunched . 对于Windows Store应用程序,您需要使用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: 要在ClickOnce应用程序中读取参数,请使用:

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. 另外,不确定,但是您可能需要通过.appref-ms快捷方式(而不是.application文件本身)运行ClickOnce应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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