SplashScreen.Close()窃取了MainWindow的焦点

Mui*_*uis 4 .net wpf focus splash-screen

当SplashScreen关闭时(手动或通过AutoClose),它会在淡出动画期间窃取MainWindow的焦点.这导致主窗口的标题从活动切换到非活动(灰色)到活动.是否有任何技巧可以防止SplashScreen窃取焦点?

VVS*_*VVS 5

告诉SplashScreen MainWindow是它的父窗口.当子窗口失去焦点时,其父窗口会获得焦点.如果没有父级,则窗口管理器决定.

splashScreen.Show(主窗口);

编辑:

我刚发现有一个SplashScreen类.看起来你使用那个类而不仅仅是我假设的普通表格.

所以,我刚用SplashScreen制作了一个简单的WPF应用程序,对我来说,上面提到的效果并没有发生.主窗口没有失去焦点.

我建议你评论应用程序的初始化代码的药水,直到闪烁停止.然后你有了一个起点,可以进一步研究为什么失去焦点.

编辑2:

在不知道你的代码的情况下,我试图重现这种现象并且并不太难.无论我尝试什么,焦点变化总是在主窗口已经显示并具有焦点时发生.

所以我看到的最佳解决方案是调用启动画面的Close()方法手动显示主窗口:

  1. 从App.xaml中删除StartupUri

  2. 启动应用程序并初始化资源后显示SplashScreen.在(当前固定的)延迟后关闭SplashScreen并显示主窗口:


public partial class App : Application
{
    const int FADEOUT_DELAY = 2000;

    SplashScreen splash = new SplashScreen("splash.jpg");

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        splash.Show(false, true);

        var worker = new BackgroundWorker();
        worker.DoWork += (sender, ea) =>
            {
                Thread.Sleep(1000);
                splash.Close(new TimeSpan(0, 0, 0, 0, FADEOUT_DELAY));
                // you could reduce the delay and show the main window with a nice transition
                Thread.Sleep(FADEOUT_DELAY); 
                Dispatcher.BeginInvoke(new Action(() => MainWindow.Show()));
            };

        worker.RunWorkerAsync();

        MainWindow = new MainWindow();

        // do more initialization
    }
}
Run Code Online (Sandbox Code Playgroud)