2011-08-26 55 views
5

我想知道當我的程序以命令行參數開始(即傳遞文件名)時,是否可以'自動加載我的主窗口'關閉'。我遇到的問題是我的程序在與它關聯的文件被點擊時加載,但是通過打開另一個主窗口並使用它來加載。我遇到的問題是,程序之後仍會啓動MainWindow,從而打開兩個Windows,一個是文件內容,一個是空的。C# - 無窗口程序

如何防止空白窗口?正如我所看到的,我要麼停止打開主窗口,關閉主窗口或讓程序將文件傳遞到主窗口。我的問題是,我不知道哪些是最好的或者如何去做。

這是代碼:

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. 
     } 
    } 

編輯:

我的解決辦法是在底部。

+0

閱讀命令行參數的一部分,如果它是一些值,不顯示主窗口不起作用? –

回答

5

我假設你使用WPF?您需要替換WPF爲您提供的入口點(Main)。然後,您可以啓動WPF或不依賴於命令行參數。看到這個問題的更多信息:

Replacing the WPF entry point

6

我相信你可以用自己的Main方法添加一個單獨的類,並將其設置爲可執行文件的入口點。然後你可以解析那裏的方法參數,或者調出主窗口。

(我假設這是一個WPF應用程序 - 這是一個WinForms應用程序簡單,因爲你可以直接修改原有的主要方法。)

+0

謝謝。是的,這是WPF。我想我會遵循,會在早上檢查並回來。 – user646265

+0

請參閱Qwertie關於如何替換WPF生成的'Main'方法的答案。 –

+1

@Merlyn:並不是所引用問題的接受答案是,儘管你可以這樣做,但回答者仍建議創建一個單獨的類,而不是:) –

1

我重寫代碼如下:

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)只是將數據加載到當前實例中。

1

大家好,感謝回去我,對不起,我不回來越快。 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"--> 

再次感謝大家的答案。感謝大家。

0

我編輯app.xmal以刪除起始URL。然後我編輯app.xaml.cs併爲App添加構造函數並在那裏處理我的處理 - 我使用「Shutdown()」關閉應用程序。

您可以根據需要打開窗戶。當我啓動其他窗口時,我使用OnStartup事件來完成它...