簡體   English   中英

我怎樣才能制作出一個出色的啟動屏幕,同時執行兩個操作?

[英]How can I make a good splash screen that does two actions at once?

我目前正在嘗試制作啟動畫面,但是,我似乎無法一次完成一些任務設置工作。

我嘗試過使用BackgroundWorker類以及Thread類,但似乎都沒有用。

在App.xaml.cs中:

protected override void OnStartup(StartupEventArgs e)
{
    var splashScreen = new Windows.Splash();
    splashScreen.Show();

    base.OnStartup(e);

    splashScreen.Close();
}

在splashScreen.xaml.cs中:

public Splash()
{
    InitializeComponent();
    DataContext = this;

    changeLoadingTxtTimer = new System.Timers.Timer(2000);
    changeLoadingTxtTimer.Elapsed += Timer_Elapsed;

    backgroundWorker = new BackgroundWorker();
    backgroundWorker.DoWork += D_DoWork;
    backgroundWorker.RunWorkerAsync();

    changeLoadingTxtTimer.Start();
}

private void D_DoWork(object sender, DoWorkEventArgs e) { UpdateDatabase(); }

private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    LoadingTxtValue = LoadingTxts[rd.Next(0, LoadingTxts.Length - 1)];

    if (!backgroundWorker.IsBusy)
        changeLoadingTxtTimer.Stop();
    }
}

我希望在BackgroundWorker工作時,加載文本每2秒更改一次,但實際上發生的是BackgroundWorker完成其工作,並且初始屏幕關閉。

App.xaml中

刪除StartupUri條目

<Application
      x:Class="WpfSplashApp.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:WpfSplashApp">
    <Application.Resources />
</Application>

App.xaml.cs

public partial class App : Application
{
    public App()
    {
        Startup += App_Startup;
    }

    private async void App_Startup( object sender, StartupEventArgs e )
    {
        var splash = new SplashWindow();
        splash.Show();

        await InitializeAsync();

        var main = new MainWindow();
        main.Show();
        MainWindow = main;

        splash.Close();            
    }

    private Task InitializeAsync()
    {
        // Do some ASYNC initialization 
        return Task.Delay( 5000 );
    }
}

如果我理解正確,那么您有一個字符串列表:

var TextList = new[] {"Text 1", "Text 2", "Text 3"};

而且,在進行后台作業時,您希望這些字符串每2秒顯示一次。 我不知道您的后台工作者會做什么,但是我假設您知道您在做什么。 我還假定您的Textblock綁定到LoadingTxtValue上名為LoadingTxtValue的字符串屬性。

我認為這項工作的最佳工具是Reactive 添加對System.Reactive的引用,並在RunWorkerAsync之后使用以下內容。

Observable.Timer(TimeSpan.FromSeconds(2))
.Subscribe(
    t => App.Current.Dispatcher.Invoke(new Action(() => LoadingTxtValue = TextList[t])));

我希望這可以解決您的問題。

我認為您應該在主視圖本身內部進行主視圖的初始化。 一旦主視圖被初始化,它將引發一個相應的事件。 然后,您通過關閉啟動屏幕並顯示主視圖來對此事件做出反應。 因此,初始屏幕的唯一任務是顯示初始內容。

App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    this.splashScreen = new Windows.Splash();
    this.splashScreen.Show();

    // Initialize the main application view
    this.MainWindow = new MainWindow();
    this.MainWindow.Initialized += ShowMainWindow;
    this.MainWindow.Initialize();
}

private void ShowMainWindow(object sender, EventArgs e)
{
    this.splashScreen.CloseSplashScreen();
    this.MainWindow.Show();
}

Splash.xaml.cs:

public Splash()
{
    InitializeComponent();
    this.DataContext = this;

    this.changeLoadingTxtTimer = new System.Timers.Timer(2000);
    this.changeLoadingTxtTimer.Elapsed += Timer_Elapsed;
    this.changeLoadingTxtTimer.Start();
}

public void CloseSplashScreen()
{
    this.changeLoadingTxtTimer.Stop();
    this.changeLoadingTxtTimer.Dispose();
    Close();
}

private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    this.Dispatcher.Invoke(() => this.LoadingTxtValue = this.LoadingTxts[rd.Next(0, this.LoadingTxts.Length - 1)]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM