简体   繁体   English

我怎样才能制作出一个出色的启动屏幕,同时执行两个操作?

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

I'm currently trying to make a splash screen, however, I can't seem to be able to make a few tasks setup things at once. 我目前正在尝试制作启动画面,但是,我似乎无法一次完成一些任务设置工作。

I've tried using the BackgroundWorker class, as well as the Thread class, and none seem to work. 我尝试过使用BackgroundWorker类以及Thread类,但似乎都没有用。

In the App.xaml.cs: 在App.xaml.cs中:

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

    base.OnStartup(e);

    splashScreen.Close();
}

In the splashScreen.xaml.cs: 在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();
    }
}

I expect that as the BackgroundWorker works, the loading text will change every 2 seconds, but what actually happens is that the BackgroundWorker finishes its job, and the splash screen closes. 我希望在BackgroundWorker工作时,加载文本每2秒更改一次,但实际上发生的是BackgroundWorker完成其工作,并且初始屏幕关闭。

App.xaml App.xaml中

Remove the StartupUri entry 删除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 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 );
    }
}

If I understand correctly, you have a list of strings: 如果我理解正确,那么您有一个字符串列表:

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

And while a background job is happening, you want those strings to show up every 2 seconds. 而且,在进行后台作业时,您希望这些字符串每2秒显示一次。 I have no idea about what your background worker does, but I'll assume you know what you're doing with it. 我不知道您的后台工作者会做什么,但是我假设您知道您在做什么。 I also assume your Textblock is bound to a string property called LoadingTxtValue on your your control. 我还假定您的Textblock绑定到LoadingTxtValue上名为LoadingTxtValue的字符串属性。

The best tool in my opinion for this job is Reactive . 我认为这项工作的最佳工具是Reactive Add a reference to System.Reactive , and use the following right after RunWorkerAsync . 添加对System.Reactive的引用,并在RunWorkerAsync之后使用以下内容。

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

I hope this can solve your problem. 我希望这可以解决您的问题。

I think you should do the initialization of the main view inside the main view itself. 我认为您应该在主视图本身内部进行主视图的初始化。 Once the main view is initialized, it would raise a corresponding event. 一旦主视图被初始化,它将引发一个相应的事件。 You then react to this event by closing the splash screen and showing the main view. 然后,您通过关闭启动屏幕并显示主视图来对此事件做出反应。 So the only task of the splash screen is to display the splash content. 因此,初始屏幕的唯一任务是显示初始内容。

App.xaml.cs: 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: 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