我有一个C#/ WPF项目,它代表一个dbf编辑器,允许您以三种方式打开文件: - 使用工具栏中的apposit按钮 - 拖曳&下降 - 双击文件 现在我使用一个包含每个dbf open的TabControl。 我可以使用内部按钮处理它并将项目拖动到容器。 如果我通过双击打开一个文件,并且有一个打开的实例,我想将它添加到容器中,而是打开一个新实例。 我的代码:
公共部分类App:应用程序 {
private static Editor mainWindow = null;
protected override void OnStartup(StartupEventArgs e)
{
Editor mainWindow = new Editor(e.Args);
mainWindow.Show();
}
}
编辑:
public partial class Editor:Window
{
ChooseMessage.Choose choose;
public Dictionary<int, DBFStructure> ds;
string DbfName;
private string[] OldNew;
public Editor(string[] e)
{
InitializeComponent();
ds = new Dictionary<int, DBFStructure>();
OldNew = new string[2];
choose = ChooseMessage.Choose.OK;
if (e.Length > 0)
if (File.Exists(e[0]) && e[0].EndsWith(".dbf", StringComparison.InvariantCultureIgnoreCase))
EffOpen(e[0]);
}
private void dbf_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, true) == true)
{
string filename = ((string[])e.Data.GetData(DataFormats.FileDrop, true))[0];
if (File.Exists(filename) && filename.EndsWith(".dbf", StringComparison.InvariantCultureIgnoreCase))
EffOpen(filename);
}
}
....
}
我为代码显示道歉,但我无法正确设置。
我的问题是拦截从最后一个打开的编辑器实例打开dbf并将其添加到controltab,否则创建一个新实例。
P.S。 EffOpen(filename)表示通过传递fil名称加载它并将其添加到容器中的方法
感谢所有
答案 0 :(得分:0)
双击打开文件时,Windows将启动一个新的应用程序实例。这是设计,你不能改变这一点。
在这个新实例中,您没有直接引用第一个应用程序实例的对象,因此您不能只将正在打开的文件添加到第一个实例的TabControl
。
您需要像这样实现它:
Mutex
来执行此操作;尝试在应用启动时创建Mutex
并检查out
构造函数的Mutex(bool, string, out bool)
参数 - 如果它是false
,那么已经有一个实例NamedPipe
将是一种非常简单的方法