-1
我想用c#构建一个实例WPF应用程序。我按照this answer创建以下应用程序。但是,无论我重新启动相同的应用程序多少次(双击* .exe文件),OnStartupNextInstance函数都不会被调用。OnStartupNextInstance不会被调用
调试输出中没有任何例外或任何内容。
有没有人有使用这种方法工作的单个实例WPF应用程序?我在这里错过了什么?
using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Windows;
namespace WpfApp1
{
public class EntryPoint
{
[STAThread]
public static void Main(string[] args)
{
var man = new SingleInstanceManager();
man.Run(args);
}
}
public class SingleInstanceManager : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
public SingleInstanceManager()
{
IsSingleInstance = true;
}
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
{
MessageBox.Show("First time launch");
var app = new App();
app.InitializeComponent();
app.Run();
return false;
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
MessageBox.Show("Subsequent launch");
base.OnStartupNextInstance(eventArgs);
eventArgs.BringToForeground = true;
}
}
}
在你的链接中,它建议使用'public void Activate()'方法来激活MainWindow(并在OnStartupNextInstance中调用它),这可能是相关的吗?你也可以尝试在OnStartupNextInstance的第一行放置一个断点,并检查调试模式,如果你的代码到达那里。 –
你确定你正在调试正确的实例吗? 'OnStartupNextInstance()'方法仅在原始进程中调用,而不是新进程。注意'MessageBox.Show()'。您应该看到该消息框启动后续实例,并且您发布的代码中没有任何内容会提示发生任何不同的事情。你确定你发布的代码实际上是你正在运行的代码吗? –
@KeyurPATEL WPF应用程序没有激活功能:( – Believe2014