WPF 命令行 [英] WPF Command Line

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

问题描述

我正在尝试创建一个接受命令行参数的 WPF 应用程序.如果没有给出参数,则应弹出主窗口.在某些特定命令行参数的情况下,代码应该在没有 GUI 的情况下运行并在完成后退出.任何有关如何正确执行此操作的建议将不胜感激.

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.

推荐答案

首先,在 App.xaml 文件的顶部找到这个属性并将其删除:

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.

接下来,覆盖您的 App 类中的 OnStartup 方法以执行逻辑:

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天全站免登陆