簡體   English   中英

C# - 沒有窗口的程序

[英]C# - Program without a Window

我想知道當我的程序以命令行參數啟動時(即傳遞文件名時)是否可以“關閉”我的主窗口自動加載。 我遇到的問題是我的程序在單擊與之關聯的文件時加載,但是通過打開另一個主窗口並使用它來執行此操作。 我遇到的問題是該程序后來仍然啟動MainWindow,從而打開兩個Windows,一個包含文件內容,另一個包含空。

如何防止空白窗口? 在我看來,我要么阻止它打開主窗口,關閉主窗口或讓程序將文件傳遞給主窗口。 我的問題是,我不知道哪些是最好的或如何做到這一點。

這是代碼:

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

編輯:

我的解決方案在底部。

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

(我假設這是一個WPF應用程序 - 它在WinForms應用程序中更簡單,因為您可以直接修改原始Main方法。)

我假設你使用WPF? 您需要替換WPF為您提供的入口點(Main)。 然后,您可以啟動WPF,具體取決於命令行參數。 有關詳細信息,請參閱此問題:

替換WPF入口點

我會按如下方式重寫您的代碼:

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

大家好,謝謝你回復我,對不起,我早點不回來。 Nate所說的部分內容是正確的,因為我需要先調用我的Window然后,如果有命令行參數,則解析文件名。 我看到的問題是它之后仍然啟動了一個主窗口,因為它被設置為我的啟動,所以我使用Qwertie建議的信息來改變我的app.xaml,這意味着它指向了一個不同的啟動,反過來意味着Window沒有被不必要地打開。

在App.xaml.cs中的“App:Application”類中:

    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"-->

再次感謝大家的回答。 關心大家。

我編輯app.xmal以刪除起始URL。 然后我編輯app.xaml.cs並為App添加構造函數並在那里進行處理 - 我使用“Shutdown()”來關閉應用程序。

您可以根據需要打開窗口。 當我啟動其他窗口時,我使用OnStartup事件來做...

從APP.XAML頁面中刪除WindowUri。 這不會顯示任何窗口。 另外,在app()構造函數或啟動事件上添加邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM