【问题标题】:C# - Program without a WindowC# - 没有窗口的程序
【发布时间】:2011-08-26 21:40:37
【问题描述】:

我想知道当我的程序以命令行参数启动时(即传递文件名时),是否可以“关闭”我的主窗口自动加载。我遇到的问题是我的程序在单击与其关联的文件时加载,但是通过打开另一个主窗口并使用它来实现。我遇到的问题是程序之后仍然启动 MainWindow,因此打开了两个窗口,一个带有文件内容,一个是空的。

如何防止空白窗口?如我所见,我要么阻止它打开主窗口,关闭主窗口,要么让程序将文件传递给主窗口。我的问题是我不知道其中哪一个是最好的或如何去做。

这是代码:

    protected override void OnStartup(StartupEventArgs e)
    {
        if (e.Args != null && e.Args.Count() > 0)
        {
            this.Properties["ArbitraryArgName"] = e.Args[0];
        }
        base.OnStartup(e);

        if (Application.Current.Properties["ArbitraryArgName"] != null)
        {

            string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
            MainWindow mw = new MainWindow();
            mw.Show();
            mw.readVcard(fname);
            Application.Current.Windows.
        }
    }

编辑:

我的解决方案在底部。

【问题讨论】:

  • 读取命令行参数的哪一部分,如果它是某个值,不显示主窗口不起作用?

标签: c# wpf command-line mainwindow


【解决方案1】:

我相信您可以使用自己的 Main 方法添加一个单独的类,并将其设置为可执行文件的入口点。然后你可以在那里解析方法参数,或者调出或不调出主窗口。

(我假设这是一个 WPF 应用程序 - 它在 WinForms 应用程序中更简单,因为您可以直接修改原始 Main 方法。)

【讨论】:

  • 谢谢。是的,它是WPF。我想我会关注,会检查并在早上回来。
  • 查看 Qwertie 的回答,了解如何替换 WPF 生成的 Main 方法。
  • @Merlyn:对于引用的问题,接受的答案并不是说虽然你可以这样做,但回答者仍然建议创建一个单独的类 :)
【解决方案2】:

我假设您使用 WPF?您需要替换 WPF 为您提供的入口点 (Main)。然后,您可以根据命令行参数启动或不启动 WPF。有关更多信息,请参阅此问题:

Replacing the WPF entry point

【讨论】:

    【解决方案3】:

    从 APP.XAML 页面中删除 WindowUri。 那不会显示任何窗口。此外,在 app() 构造函数或启动事件中添加您的逻辑。

    【讨论】:

      【解决方案4】:

      我会重写你的代码如下:

      protected override void OnStartup(StartupEventArgs e) 
      { 
          // start application window
          MainWindow mw = new MainWindow(); 
          mw.Show(); 
          // store argument and read card info
          if (e.Args != null && e.Args.Count() > 0) 
          { 
              this.Properties["ArbitraryArgName"] = e.Args[0]; 
              string fname = Application.Current.Properties["ArbitraryArgName"].ToString(); 
              mw.readVcard(fname); 
          } 
      } 
      

      这假设MainWindow.readVcard(string) 方法只是将数据加载到当前实例中。

      【讨论】:

        【解决方案5】:

        大家好,感谢您回复我,抱歉我没有早点回来。 Nate 所说的部分内容是正确的,因为我需要更早地调用我的 Window,然后,如果有命令行参数,则解析文件名。我看到的问题是它之后仍然启动了一个主窗口,因为它被设置为我的启动,所以我使用 Qwertie 建议的信息来更改我的 app.xaml,这意味着它指向不同的启动,它反过来意味着没有不必要地打开窗口。

        在 App.xaml.cs 中的“应用程序:应用程序”类中:

            private void OnStartUp(object sender, StartupEventArgs e)
            {
                OnStartup(e);
            }
        
            protected override void OnStartup(StartupEventArgs e)
            {
                MainWindow mw = new MainWindow();
        
                if (e.Args != null && e.Args.Count() > 0)
                {
                    this.Properties["ArbitraryArgName"] = e.Args[0];
                }
                //base.OnStartup(e);
        
                if (Application.Current.Properties["ArbitraryArgName"] != null)
                {               
                    string fname = Application.Current.Properties["ArbitraryArgName"].ToString();
        
                    mw.Show();
                    mw.readVcard(fname);
                    //Application curApp = Application.Current;
                    //curApp.Shutdown();
                }
        
                else if (e.Args.Count() == 0)
                {
                    mw.Show();
                }
            }
        

        在 App.xaml 中:

        <Application x:Class="Vcardviewer.App"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             
                     Startup="OnStartUp"
                     >
            <Application.Resources>
        
            </Application.Resources>
        </Application>
        <!--StartupUri="MainWindow.xaml"-->
        

        再次感谢大家的回答。向大家问好。

        【讨论】:

          【解决方案6】:

          我编辑 app.xmal 以删除起始 URL。然后我编辑 app.xaml.cs 并为 App 添加一个构造函数并在那里进行处理 - 我使用“Shutdown()”关闭应用程序。

          您可以根据需要打开窗口。当我启动其他窗口时,我使用 OnStartup 事件来执行它...

          【讨论】:

            猜你喜欢
            • 2011-07-15
            • 1970-01-01
            • 1970-01-01
            • 2010-09-18
            • 2021-04-30
            • 2016-02-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多