繁体   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