【问题标题】:Delaying Window for Splash Screen启动画面的延迟窗口
【发布时间】:2011-08-23 14:45:37
【问题描述】:

我正在开发一个项目,我想在其中添加一个启动画面。我已经在 Stackoverflow 和其他博客以及 MSDN 等上检查了问题,但找不到我正在寻找的解决方案。

我想要我的 SplashScreen,

1- 出现并在屏幕上停留 3-4 秒,但同时我希望我的主窗口不出现。当我的启动画面完全消失时,应该会出现主窗口。我检查过的许多示例都没有实现这一点。即使我设置了 SplashScreen.Close.(TimeSpan.FromMiliseconds(4000)) MainWindow 仍然在 SplashScreen 的前面或后面出现。他们说“将图像添加到您的项目中,使其成为 Build Action SplashScreen 或 Resource,如果您想处理淡出时间,请转到 App.xaml.cs 文件并实现您自己的 Main() 方法并放置您的逻辑。”我已经知道了。它不起作用。

2- 如果可能的话,我希望我的启动画面不要慢慢淡出。我希望它突然消失。(如果这对于中级开发人员来说不可能或真的很难做到,那没关系。你可以无视。)

我想要 C# 代码而不是 Xaml。我的项目基于 WPF 和 .NET 4.0 客户端配置文件。

谢谢。

【问题讨论】:

  • 您正在使用 WPF 并且不想使用 XAML?
  • 我更喜欢 C# 代码。我的意思是包含 C# 和 Xaml 的答案将是完美的。

标签: c# wpf splash-screen


【解决方案1】:

为什么不将初始屏幕设置为完全限定的 XAML <window> 并在 App.xaml 中将其设置为 StartupUri:

<Application x:Class="MyApp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="SplashWindow.xaml">

然后在启动窗口的加载事件中初始化主窗口(最好是在其他地方,这样当您关闭启动时实例会停留在周围)。从这里您还可以指定一个计时器,让您在 x 秒内关闭并显示主窗口/隐藏启动窗口。

using System.Threading;

/// -------------------------------

private Timer t;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    App.MainWindow = new MainWindow(); // Creates but wont show

    t = new Timer(new TimerCallback(CloseSplash), null, new TimeSpan(0,0,10), new TimeSpan(0,0,0,0,-1));

    // Do Other load stuff here?
}

private void CloseSplash(object info)
{
     // Dispatch to UI Thread
    Dispatcher.Invoke(DispatcherPriority.Normal, x => CloseSplashMain());
}

private void CloseSplashMain()
{
   App.MainWindow.Show()
   this.Close();
}

不过,您必须更改应用的主窗口行为,否则关闭启动窗口会导致应用关闭。

public partial class App : Application
{
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        App.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
    }
}

完成后不要忘记丢弃计时器。这是一个IDisposable,除非它被停止,否则它将继续触发该方法。

【讨论】:

    【解决方案2】:

    有答案,但我找到了一个更简单的答案。只需在主窗口的构造函数中使用Thread.Sleep(int miliSeconds) 方法。这将延迟您的应用,以便在指定的毫秒后打开。

    【讨论】:

    • 值得注意的是,您应该不要在 .NET 中再调用 Thread.Sleep。现在Task.Delay 方法更可取,因为它不会冻结 UI。
    【解决方案3】:

    在 App.xaml.cs 的构造函数中打开您的初始屏幕,等待几秒钟,然后关闭,然后继续执行应用程序的其余部分。我正在使用 Unity,所以在 Boostrapper 初始化了一些服务之后,我在某处关闭了启动画面。

     public partial class App : Application
        {
            private static SplashScreen _splashScreen;
    
            public App()
            {
                OpenSplashScreen();
                new Bootstrapper().Run(); 
            }
    
            private void OpenSplashScreen()
            {
                _splashScreen = new SplashScreen("SplashScreen/splash.jpg");
                _splashScreen.Show(false);
            }
    
            internal static void CloseSplashScreen(double time)
            {
                _splashScreen.Close(TimeSpan.FromSeconds(0));
                _splashScreen = null;
            }
        }
    

    下面列出了 Bootstrapper.cs:

    public class Bootstrapper : UnityBootstrapper
        {
            protected override void ConfigureContainer()
            {
                base.ConfigureContainer();
                var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(Container);
    
               // initialize some services before
    
                App.CloseSplashScreen(0);
    
            }
    
            protected override IModuleEnumerator GetModuleEnumerator()
            {
                return new ExtendedConfigurationModuleEnumerator();
            }
    
            protected override DependencyObject CreateShell()
            {
                MainWindow shell = new MainWindow(Container);
                shell.Show();
                return shell;
            }
        }
    

    【讨论】:

      【解决方案4】:

      使用 API 的最佳方式是

        SplashScreen splash = new SplashScreen("splashscreen.jpg");
        splash.Show(false);
        splash.Close(TimeSpan.FromMilliseconds(2));
        InitializeComponent();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-22
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多