我有一个C#窗体,使用Microsoft提供的教程设置为SingleInstance:Windows Forms single instance application (CSWinFormSingleInstanceApp)。我遇到的问题是获得在第一个之后添加的任何其他参数。
的Program.cs:
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SingleInstanceAppStarter.Start((args.Length == 0
? new frm_remPhotoViewer(string.Empty)
: new frm_remPhotoViewer(args[0])),
StartNewInstance);
}
static void StartNewInstance(object sender, StartupNextInstanceEventArgs e)
{
FormCollection forms = Application.OpenForms;
//MessageBox.Show(e.CommandLine[1]);
if (forms["frm_remPhotoViewer"] != null
&& forms["frm_remPhotoViewer"].WindowState == FormWindowState.Minimized)
{
forms["frm_remPhotoViewer"].WindowState = FormWindowState.Normal;
forms["frm_remPhotoViewer"].Activate();
}
else if (forms["frm_remPhotoViewer"] == null)
{
frm_remPhotoViewer f = new frm_remPhotoViewer("HELP ME!!!");
f.ShowDialog();
}
}
SingleInstanceAppStarter.cs:
static SingleInstanceApp app = null;
// Construct SingleInstanceApp object, and invoke its run method.
public static void Start(Form f, StartupNextInstanceEventHandler handler)
{
if (app == null && f != null)
app = new SingleInstanceApp(f);
// Wire up StartupNextInstance event handler.
app.StartupNextInstance += handler;
app.Run(Environment.GetCommandLineArgs());
}
SingleInstanceAppHelper.cs:
public SingleInstanceApp()
{
}
public SingleInstanceApp(Form f)
{
// Set IsSingleInstance property to true to make the application
base.IsSingleInstance = true;
// Set MainForm of the application.
this.MainForm = f;
}
注释掉的行是我要添加到活动表单的文本。
答案 0 :(得分:1)
我可能不完全明白你在第一个之后获得额外的参数是什么意思......但是你认为你是指通过
命令行参数void Main(string[] args)
您正在使用。
创建表单new frm_remPhotoViewer(string.Empty)
or
new frm_remPhotoViewer(args[0])
但似乎想要args [0],args [1]等的可能性......
为什么不直接将表单的构造函数参数从单个字符串设置为接受字符串的ARRAY
public YourFormConstructor( string someParm )
to
public YourFormConstructor( string[] someParmArray )
然后,在你的表单的构造函数中,你可以对数组的长度进行测试,并做任何你需要的东西。例如:
public YourFormConstructor( string[] someParmArray )
{
if( someParmArray.Length == 0 )
form.SomeProperty = string.Empty;
else
{
foreach( string s in someParmArray )
{
// do something based on each string "s" provided..
if( s.StartsWith( "-aCommandLineArgumentFlag1" ))
blah...
if( s.StartsWith( "-aDiffCommandLineArgumentFlag" ))
blah...
}
}
}
答案 1 :(得分:-1)
static class Program
{
static MainForm mainForm;
[STAThread]
static void Main(params string[] Arguments)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mainForm = new MainForm(Arguments);
SingleInstanceApplication.Run(mainForm, NewInstanceHandler);
}
public static void NewInstanceHandler(object sender, StartupNextInstanceEventArgs e)
{
mainForm.FileName(e.CommandLine[1]);
e.BringToForeground = false;
}
public class SingleInstanceApplication : WindowsFormsApplicationBase
{
private SingleInstanceApplication()
{
base.IsSingleInstance = true;
}
public static void Run(RibbonForm f, StartupNextInstanceEventHandler startupHandler)
{
SingleInstanceApplication app = new SingleInstanceApplication();
app.MainForm= f;
if (f.DialogResult != DialogResult.Cancel)
{
app.StartupNextInstance += startupHandler;
app.Run(Environment.GetCommandLineArgs());
}
}
}
//add this to MainForm
public void FileName(string args)
{ }