To start with, we need someplace to put the command line parameters. I use a global static List<string> for this. I know, globals are bad, and that's true--but in my view, it is perfectly OK to use a very small number of global variables for things that you know you'll only need one of in an application. If you don't want to do this, that's fine. Use a field/property in your main window or add it to your Application class. But here's what I have in Globals.cs:
namespace MyApplication
{
public static class Globals
{
public static List<string> Args;
}
}
Filling the list is simple. All you have to do is override Application.OnStartup in App.xaml.cs and look at the System.Windows.StartupEventArgs parameter. StartupEventArgs contains an Args member, which is an array of strings containing your command line parameters. Like this:
namespace MyApplication
{
public partial class App : Application
{
...
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Globals.Args = new List();
for (int ix = 0; ix < e.Args.GetLength(0); ++ix)
Globals.Args.Add(e.Args[ix]);
}
}
}
No comments:
Post a Comment