C # - a program without a window

I am wondering if it is possible to automatically disconnect my main window from automatic loading when my program starts with a command line argument (i.e. when the file name is passed). The problem is that my program loads when I click the file associated with it, but does this by opening another main window and using it. The problem is that the program still starts MainWindow after that, opening two Windows, one with the contents of the file and an empty one.

How to prevent a blank window? As I see it, I either stop it by opening the main window, closing the main window, or make the program transfer the file to the main window. My problem is that I donโ€™t know which ones would be the best or how to do it.

This is the code:

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

EDIT:

My solution is below.

+5
5

, WPF? (Main), WPF . WPF , . . :

WPF

+5

, Main . , .

( , WPF - WinForms, Main.)

+6

:

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

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

. .

+1

I am editing app.xmal to remove the start URL. Then I edit app.xaml.cs and add a constructor for the application and do my processing there - I use "Shutdown ()" to close the application.

You can open windows as needed. When I launch other windows, I use the OnStartup event to do this ...

0
source

All Articles