WPF命令行 [英] WPF Command Line

查看:177
本文介绍了WPF命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个接受命令行参数的WPF应用程序。如果没有给出参数,主窗口应该弹出。在一些特定的命令行参数的情况下,代码应该运行没有GUI和完成后退出。

解决方案

首先,在应用程序的顶部找到此属性。 xaml文件并删除它:

  StartupUri =Window1.xaml
pre>

这意味着应用程序不会自动实例化您的主窗口并显示它。



覆盖App类中的OnStartup方法来执行逻辑:

  protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

if(/ * test command-line params * /)
{
/ *做没有GUI的东西* /
}
else
{
new Window1()。ShowDialog();
}
this.Shutdown();
}


I am trying to create a WPF application that takes command line arguments. If no arguments are given, the main window should pop up. In cases of some specific command line arguments, code should be run with no GUI and exit when finished. Any suggestions on how this should properly be done would be appreciated.

解决方案

First, find this attribute at the top of your App.xaml file and remove it:

StartupUri="Window1.xaml"

That means that the application won't automatically instantiate your main window and show it.

Next, override the OnStartup method in your App class to perform the logic:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    if ( /* test command-line params */ )
    {
        /* do stuff without a GUI */
    }
    else
    {
        new Window1().ShowDialog();
    }
    this.Shutdown();
}

这篇关于WPF命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆