rei*_*113 9 c# wpf timespan splash-screen event-listener
C#WPF应用程序
我通过使用在启动时显示SplashScreen最少的时间
Thread.Sleep(int); //int = milliseconds to display splash screen
达到该睡眠时间后,代码将恢复,并且SplashScreen将逐渐淡出以关闭
SplashScreen.Close(Timespan.FromMilliseconds(int)); //int = milliseconds fade-out
我想在此时暂停等待SplashScreen变为100%透明并完全关闭,然后继续执行其他任务,IE写入控制台或显示MainWindow.
(TimeSpan.FromMilliseconds(int))完成时是否触发了事件?还有其他建议吗?
namespace StartupSplash
{
    public class SplashScreenStartup
    {
        //IMPORTANT:set image property to Resource and NOT Splash Screen
        private SplashScreen Splash = new SplashScreen("Resources/SplashScreen.png");
        public void SplashScreenStartUp()
        {
            Splash.Show(false, true);
            Thread.Sleep(3000); // Pause code, display splash screen 3 seconds
            Splash.Close(TimeSpan.FromMilliseconds(3000)); // 3 second splash fade-out
            // I want to wait until splash screen fadeOut has completed before
            // this next console output is performed.
            Console.WriteLine("Executes before Splash fadeOut completes.");
        }
    }
在 TimeSpan 完成后,我从未找到要监听的事件。另外,在决定不停止线程后,我选择使用 DispatcherTimers。
(我已将逻辑细化并包含到这一类中以供参考)
using System;
using System.Windows;
using System.Windows.Threading;
namespace StartupSplash2
{
public partial class MainWindow : Window
{
    private DispatcherTimer visibleTimer;
    private DispatcherTimer fadeoutTimer;
    private SplashScreen splash;
    private int visibleTime = (4000); //milliseconds of splash visible time
    private int fadeoutTime = (1500); //milliseconds of splash fadeout time
    public MainWindow()
    {   
        //hide this MainWindow window until splash completes
        this.Visibility = Visibility.Hidden; 
        InitializeComponent();
        splashIn(); //start the splash
    }
    private void splashIn()
    {
        splash = new SplashScreen("Resources/SplashScreen.png"); //ensure image property is set to Resource and not screen saver
        visibleTimer = new DispatcherTimer(); //timer controlling how long splash is visible
        visibleTimer.Interval = TimeSpan.FromMilliseconds(visibleTime);
        visibleTimer.Tick += showTimer_Tick; //when timer time is reached, call 'showTimer_Tick" to begin fadeout
        splash.Show(false, true); //display splash
        visibleTimer.Start();
    }
    private void showTimer_Tick(object sender, EventArgs e)
    {
        visibleTimer.Stop();
        visibleTimer = null; //clear the unused timer
        fadeoutTimer = new DispatcherTimer();
        fadeoutTimer.Interval = TimeSpan.FromMilliseconds(fadeoutTime); //a timer that runs while splash fades out and controlls when main window is displayed
        fadeoutTimer.Tick += fadeTimer_Tick; //when fadeout timer is reached, call 'fadeTimer_Tick' to show main window
        splash.Close(TimeSpan.FromMilliseconds(fadeoutTime)); //begin splash fadeout to close
        fadeoutTimer.Start();
    }
    private void fadeTimer_Tick(object sender, EventArgs e)
    {
        fadeoutTimer.Stop();
        fadeoutTimer = null; //clear the unused timer
        splash = null; //clear the splash var
        MainWindowReady(); //call method to display main window
    }
    public void MainWindowReady()
    {
        this.Visibility = Visibility.Visible;
        //Here is the start of the Main Window Code
        this.Content = "Ok, the app is ready to roll";
    }
  }
}
| 归档时间: | 
 | 
| 查看次数: | 2203 次 | 
| 最近记录: |