9

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.");
        }

    }
4

5 回答 5

3

也许这段代码可以帮助你。使用 backgroundworker 类:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (o, ea) => 
{
   // Dispatcher.Invoke commands the dispatcher to do something
   Dispatcher.Invoke((Action)(() => Splash.Close(TimeSpan.FromMilliseconds(3000)));
   // Sleeps this worker but NOT the UI
   Thread.Sleep(3000);
};
worker.RunWorkerCompleted += (o, ea) =>
{
    // Open your mainwindow sample
    MainWindow w = new MainWindow();
    w.Show();
};

//Runs the worker on its own thread
worker.RunWorkerAsync();

这应该开始关闭您的启动画面,然后通过它进入睡眠状态,完成后它将打开您的主窗口。我实际上使用与此非常相似的东西来实现我的 WPF 应用程序的登录和获取信息,同时显示一个进度条并将其中的文本更新为“连接到服务器”、“登录”和“获取数据”之类的内容。

于 2012-11-04T17:16:04.040 回答
1

我发现以下代码有效。我不太清楚为什么,我将深入研究以更好地理解这一点。

如有需要请批评指正,我是来学习和分享的。干杯。

class Tester
    {
    // Create splash screen instance and reference the image location.
    // IMPORTANT Ensure that the image properties are set to Resource and NOT Splash Screen
    private SplashScreen Splash = new SplashScreen("Resources/SplashScreen.png");

    public void Display()
    {
        Splash.Show(false, true);
        // pause the code, thus, displaying the splash for 3 seconds
        Thread.Sleep(3000); 
        // close the splash
        Close();
    }

    private void Close()
    {
        // sets the fadeout time in milliseconds
        int fadeOutTime = 1500; 

        // wait until the splash screen fadeOut has completed before writing to the console
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += (o, ea) =>
        {
            // Run background task (fade out and close the splash)
            Splash.Close(TimeSpan.FromMilliseconds(fadeOutTime));
            // Sleep this worker but NOT the UI (for the same time as the fade out time)
            Thread.Sleep(fadeOutTime);
        };
        worker.RunWorkerCompleted += (o, ea) =>
        {
            // Execute task after splash has closed completely
            Console.WriteLine("This is after the splash screen fadeOut completes.");
        };
        // start the background task, on it's own thread
        worker.RunWorkerAsync(); 
    }

}
于 2012-11-05T05:48:53.900 回答
0

我最终得出结论,我在之前的评论中叫错了树。在后台显示 SplashScreen 既有问题(无论我尝试什么,它都拒绝自动关闭)和不必要的。这就是我最终得到的……真的很简单!

using System;
using System.Net;
using System.Windows;

namespace WpfApplication1
{
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
  {
    public Window1() {
      InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e) {
      // show the splash screen
      // nb: Resources/SplashScreenImage.png file Properties ~ Build Action='Resource'
      var splashScreen = new SplashScreen("Resources/SplashScreenImage.png");
      splashScreen.Show(false); // don't close automatically
      // ... initialise my application ...
      Initialise();
      // close the splash screen.
      splashScreen.Close(TimeSpan.FromMilliseconds(250D));
    }

    private void Initialise() {
      // do my long-running application initialisation on the main thread. 
      // In reality you'd do this download asyncronously, but in this case
      // it serves as a simple proxy for some "heavy" inititalisation work.
      textBox1.Text = new WebClient().DownloadString("http://stackoverflow.com/questions/13213625/splashscreen-closetimespan-frommilliseconds-listen-for-closed-event");
    }
  }

}

我希望这会有所帮助......虽然我完全不相信它会;-)

干杯。基思。

PS:我想知道为什么飞溅拒绝关​​闭?我猜它在内部依赖于仅在 WPF 的 event-dispatch-thread 等效项(无论它被称为什么)上可用(即可订阅)的事件。

于 2012-11-05T08:06:55.177 回答
0

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

  }
}
于 2012-11-12T04:02:02.253 回答
0

我发现了一个名为 SplashScreen.Dismissed 的事件,它允许您在 SplashScreen 过期后启动应用程序。但是,最低要求的操作系统是 Windows 8,我无法使用它。更多信息可以在这里找到MSDN

于 2012-11-16T13:21:10.997 回答