【问题标题】:How to open different windows based on command line arguments?如何根据命令行参数打开不同的窗口?
【发布时间】:2015-07-04 23:05:12
【问题描述】:

我正在尝试为 Windows 创建简单的 WPF 应用程序。我需要在从命令行获取参数时显示 Window1 并在没有参数的情况下显示 Window2。我还需要将此参数传递给 Window1。

我创建窗口并在 App 类中定义 Application_Startup() 方法:

private void Application_Startup(object sender, StartupEventArgs e)
{
    if (e.Args.Length > 0)
    {
        // Here is I need to open Window1 and pass argument to this windows class
    }
    else
    {
        // Here is I need to open Window2
    }
}
  1. 如何根据参数打开不同的窗口?
  2. 如何将 App 类的参数传递给窗口类?

【问题讨论】:

    标签: c# wpf windows


    【解决方案1】:

    有很多方法可以做到这一点,但一种方法是通过其构造函数将数据传递给新窗口类。

    像这样:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        if (e.Args.Length > 0)
        {
            var window1 = new Window1(e.Args[0]);
            window1.Show()
        }
        else
        {
             var window2 = new Window2();
             window2.Show()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 2013-08-17
      • 2015-12-23
      相关资源
      最近更新 更多