gpt4 book ai didi

.net - SplashScreen.Close() 窃取 MainWindow 的焦点

转载 作者:行者123 更新时间:2023-12-05 00:02:39 26 4
gpt4 key购买 nike

当 SplashScreen 关闭时(手动或通过 AutoClose),它会在淡出动画期间窃取 MainWindow 的焦点。这会导致主窗口的标题从事件切换到非事件(灰色)再到事件。有什么技巧可以防止 SplashScreen 窃取焦点吗?

最佳答案

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

splashScreen.Show(mainWindow);

编辑 :

我刚刚发现有一个 SplashScreen类(class)。看起来您使用该类,而不仅仅是我假设的普通表单。

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

我建议您将应用程序初始化代码的药水注释掉,直到闪烁停止。然后,您就有了更多研究为什么会失去焦点的起点。

编辑2 :

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

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

  • 从 App.xaml 中删除 StartupUri
  • 启动应用程序并初始化资源后显示 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
    }
    }

    关于.net - SplashScreen.Close() 窃取 MainWindow 的焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7688715/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com