0

我正在构建一个小型应用程序,它对另一个软件的程序文件执行更新,然后启动该软件。启动应用程序后,会在第二个 GUI 线程中显示启动窗口,而在主线程上构建并显示第二个应用程序的主窗口:

var shell = (Shell) Shell;
Application.Current.MainWindow = shell;
shell.Show();

显示第二个应用程序的外壳后,关闭第一个窗口。一切正常,除了第二个窗口的外壳在* 其他窗口下弹出。即:我从资源管理器启动启动器应用程序,启动器窗口显示在资源管理器窗口的顶部。启动器窗口关闭,主应用程序窗口出现在资源管理器窗口下:

我试过了:

var shell = (Shell) Shell;
Application.Current.MainWindow = shell;
shell.Topmost = true;
shell.Show();
shell.InjectInitialViews();

这解决了这个问题,但是即使我单击任务栏中的另一个应用程序,我也无法在外壳上方显示其他窗口。

var shell = (Shell) Shell;
Application.Current.MainWindow = shell;
shell.Topmost = true;
shell.Show();
shell.InjectInitialViews();
shell.Topmost = false;

什么都不做......实现这一目标的正确方法是什么?

4

3 回答 3

1

您的第二个示例没有意义,因为 UI在程序执行期间更新,因此您的调用将被忽略。实现您想要的更好的方法可能是处理和可能的事件:shell.Topmost = true;Window.DeactivatedWindow.Activated

private void SecondWindow_Deactivated(object sender, EventArgs e)
{
    MainWindow window = (MainWindow)sender;
    window.TopMost = true;
}
于 2013-11-11T13:46:01.367 回答
1

如果我没记错的话,我通过执行以下操作在带有外部启动器的 WPF 智能客户端中解决了这个问题:

  1. 启动主应用程序后,启动器AllowSetForegroundWindow通过 P/Invoke 调用以允许主应用程序设置前台窗口(如果启动器处于活动状态,则允许这样做)。

  2. Main application calls SetForegroundWindow via P/Invoke to set the foreground window to its main shell window.

...or something along those lines. It might have been the launcher who called SetForegroundWindow after waited for the app's main window to become active. This was some time ago, and I no longer have access to the source.

于 2013-11-11T13:52:42.790 回答
0

Thanks to the answers of Mike and Sheridan, I found the solution to be simply activating the window in the thread it is created in. So

var shell = (Shell) Shell;
Application.Current.MainWindow = shell;
shell.Topmost = true;
shell.Show();
shell.InjectInitialViews();
shell.Activate();

does the trick.

于 2013-11-11T14:57:06.270 回答