启动画面的延迟窗口

发布于 2024-12-01 09:17:33 字数 538 浏览 1 评论 0原文

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

我希望我的闪屏

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

2-如果可能的话,我希望我的闪屏不要慢慢淡出。我希望它突然消失。(如果这对中级开发人员来说不可能或真的很难做到,那没关系。您可以忽略。)

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

谢谢。

I am developing a project and I want to add to it a splash screen. I have checked questions here on Stackoverflow and other blogs and MSDN etc. but could not find the solution I am looking for.

I want my SplashScreen,

1- appear and stay on the screen 3-4 seconds but at the same time I want my Main Window NOT TO appear. When my splash screen fades out completely then Main Window should appear. Many of the examples I have checked out do not implement this. Even though I set SplashScreen.Close.(TimeSpan.FromMiliseconds(4000)) MainWindow still apeear immediately front or back of SplashScreen. They say "add an image to your project, make it's Build Action SplashScreen or Resource, if you want to handle fade out time go App.xaml.cs file and implement your own Main() method and put your logic." I know that already. It does not work.

2- If possible I want my splashscreen NOT TO fade out slowly. I want it to disappear suddenly.(if this is not possible or really hard for a intermediate developer to do it, it is ok. you may disregard.)

And please I want C# code not Xaml. My project is based on WPF adn .NET 4.0 client profile.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最佳男配角 2024-12-08 09:17:33

为什么不将启动屏幕设置为完全限定的 XAML 并在 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">

然后在启动窗口的 Load 事件中初始化主窗口 (最好在其他地方,这样当您关闭启动画面时实例就会保留下来)。从这里您还可以指定一个计时器,在 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 并且将继续触发该方法,除非它被停止。

Why don't you make your splash screen a fully qualified XAML <window> and in your App.xaml set it up as your 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">

Then in your splash window's Load Event you initialize the main window (preferably somewhere else so the instance sticks around when you close the splash). From here you can also specify a timer for x-seconds to go off and show the main window / hide the splash window.

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();
}

You'll have to change your app's main window behaviour though, otherwise closing the splash window will cause the app to close.

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

Also don't forget to dispose your timer when you're done. It's an IDisposable and will keep firing that method unless it's stopped.

橘香 2024-12-08 09:17:33

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

There are answers but I find a an easier one. Just use the Thread.Sleep(int miliSeconds) method in the main window's constructor. This will delay your app in order to open specified miliseconds later.

绻影浮沉 2024-12-08 09:17:33

在 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;
        }
    }

In the constructor of App.xaml.cs open your splash screen, wait for a few seconds, then close before proceeding with rest of the app. I am using Unity, so I close the splash screen somewhere after the Boostrapper has initialized some services.

 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;
        }
    }

where Bootstrapper.cs is listed below:

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;
        }
    }
花开雨落又逢春i 2024-12-08 09:17:33

最好的方法和使用 API 是

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

The best way and using the API is

  SplashScreen splash = new SplashScreen("splashscreen.jpg");
  splash.Show(false);
  splash.Close(TimeSpan.FromMilliseconds(2));
  InitializeComponent();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文