I written a text editor application in WPF: http://edi.codeplex.com/
...and would like to implement a SingleInstance feature as in Notepad++. That is, the user should be able to start the editor more than once (with different files as parameter) and the System should 1 Activate the MainWindow,
2 display the editor for editing (if started for the second time) and
3 the user should be able to start editing by typing on the keyboard
I found a way to implement the communication between the first and second instance and I even got close to activation part 3. Here however is a small problem, which is, that the MainWindow is only blinking in the taskbar and the editor is showing the current file with blinking cursor (but the keyboard focus seems to be missing (because the window title bar still has the deactivated color and typing on the keyboard does not end up in the editor).
I have tried all sorts of different implementations such as 'Keyboard.Focus(Mainwindow)' and so forth but I am somehow not getting there.
Can anyone help me with this advanced focus problem? The general app code itself should be OK since I can ALT+TAB and CTRL+TAB with the expected results... Here is the code that I am using to activate the second instance - what am I doing wrong when activating the existing MainWindow in the App.cs class?
dispatcher.BeginInvoke( new Action(delegate { var mainWindow = Current.MainWindow as MainWindow; if (mainWindow != null) { if (mainWindow.IsVisible == false) mainWindow.Show(); if (mainWindow.WindowState == WindowState.Minimized) mainWindow.WindowState = WindowState.Normal; mainWindow.Topmost = true; mainWindow.Show(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (Action)delegate { mainWindow.Activate(); mainWindow.Topmost = false; }); }