如果不查看代码,很难预测。但是,请确保涵盖以下几点。
创建一个派生自Microsoft.VisualBasic.ApplicationServices
.
WindowsFormsApplicationBase
,并用它来包装你的 WPF System.Windows.Application
。包装器通过提供您自己的Main
.
namespace SingleInstanceNamespace
{
using System;
using System.Windows;
using Microsoft.VisualBasic.ApplicationServices;
public class SingleInstanceManager : WindowsFormsApplicationBase
{
public SingleInstanceManager()
{
this.IsSingleInstance = true;
}
protected override bool OnStartup(
Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
{
base.OnStartup(eventArgs);
App app = new App(); //Your application instance
app.Run();
return false;
}
protected override void OnStartupNextInstance(
StartupNextInstanceEventArgs eventArgs)
{
base.OnStartupNextInstance(eventArgs);
string args = Environment.NewLine;
foreach (string arg in eventArgs.CommandLine)
{
args += Environment.NewLine + arg;
}
string msg = string.Format("New instance started with {0} args.{1}",
eventArgs.CommandLine.Count,
args);
MessageBox.Show(msg);
}
}
}
下一个代码块详细介绍了App.cs
定义应用程序主入口点的文件的内容:
namespace SingleInstanceNamespace
{
public class MyApp
{
[STAThread]
public static void Main(string[] args)
{
//Create our new single-instance manager
SingleInstanceManager manager = new SingleInstanceManager();
manager.Run(args);
}
}
}