开发者

C# - Program without a Window

I'm wondering whether it is possible to 'turn off' my main Window from loading automatically when my program starts with a command-line argument (i.e. when a file name is passed). The problem I have is that my program loads when a file associated with it is clicked, but does so by opening another main window and using that. The problem I have is that the program still launches the MainWindow afterwards, thus opening two Windows, one with the file contents and one that is empty.

How do I prevent the blank Window? As I see it, I either stop it from opening the main Window, close the main Window or make the program pa开发者_运维技巧ss the file to the main Window. My problem is that I don't know which of these would be the best or how to 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 at the bottom.


I believe you can add a separate class with its own Main method and set that to be the entry point of your executable. Then you can parse the method arguments there and either bring up the main window or not.

(I'm assuming this is a WPF app - it's simpler in a WinForms app as you can modify the original Main method directly.)


I assume you use WPF? You'll want to replace the entry point (Main) that WPF supplies for you. Then, you can start WPF or not depending on the command-line arguments. See this question for more info:

Replacing the WPF entry point


Remove the WindowUri from APP.XAML page. That will not show any window. Also, add your logic on app() constructor or startup event.


I'd rewrite your code as follows:

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); 
    } 
} 

This assumes that the method MainWindow.readVcard(string) simply loads data into the current instance.


Hi everyone and thanks for getting back to me, sorry I've not come back sooner. Part of what Nate said was correct in that I needed to call my Window earlier and then, if there was the command-line argument, parse the file name. The issue as I saw it was that it still started up a main Window afterwards because that was set as my startup, So I used the information suggested by Qwertie to alter my app.xaml, which meant that it pointed to a different startup, which in turn meant that the Window wasn't opened unnecessarily.

In ' App : Application ' class in 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();
        }
    }

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

Thanks again to everyone for their answers. Regards to you all.


I edit the app.xmal to remove the start URL. I then edit the app.xaml.cs and add a constructor for App 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 it...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜