资料来自网上搜集,并由本人实践整理。
步骤1:
添加项目引用Microsoft.VisualBase
步骤2:
去掉App.xaml的StartupUri属性
步骤3:
在App.xaml.cs中添加Active方法,代码如下
/// <summary>
/// 窗体激活
/// </summary>
/// <param name="CommandLine"></param>
public void Active(ReadOnlyCollection<string> CommandLine)
{
if (CommandLine.Count > 0)
{
var arg = System.Web.HttpUtility.UrlDecode(CommandLine[0]);
//TODO:正式环境去掉此提示
MessageBox.Show($"接受到参数【{arg}】");
}
if (MainWindow != null)
{
MainWindow.Activate();
MainWindow.Show();
}
else
{
MainWindow = new MainWindow();
MainWindow.Show();
}
}
步骤4:
添加SingleInstanceAppWrapper类,代码如下
class SingleInstanceAppWrapper : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
public SingleInstanceAppWrapper()
{
// Enable single-instance mode.
this.IsSingleInstance = true;
}
// Create the Wpf application class.
private App app;
protected override bool OnStartup(StartupEventArgs eventArgs)
{
app = new App();
app.Active(eventArgs.CommandLine);
app.Run();
return false;
}
// Direct multiple instances.
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
app.Active(eventArgs.CommandLine);
}
}
步骤5:
添加startup类,代码如下
public class Startup
{
[STAThread]
public static void Main(string[] args)
{
SingleInstanceAppWrapper wrapper = new SingleInstanceAppWrapper();
wrapper.Run(args);
}
}
步骤6:
设定项目启动对象为startup