带ProgressBar的WPF SplashScreen

use*_*410 4 wpf splash-screen

我有一个WPF项目,项目向导添加了一个启动画面.在同一个启动画面上,我想添加一个进度条样式表.有人知道怎么做吗?

Ali*_*ran 11

这是我做这个的计划.我这样做的动机是我不想在UI线程上运行初始化代码,通常我想在我的App类(不是Splash屏幕)上使用初始化代码.

基本上,我将应用程序设置StartupUri为我的启动画面,这使得滚动.

在启动屏幕上,我在应用程序上调用委托.这是在工作线程上运行的.在启动画面中,我处理EndInvoke,关闭窗口.

在应用程序初始化委托中,我完成工作,最后创建并打开正常的主窗口.在工作负载期间,我还有一个Slash方法,允许我更新进度.

好吧,代码相当短,并且不包括主窗口代码(它不受所有这些影响),但是它使用匿名委托进行抢救,因此请仔细阅读,并在调试器中理想地使用它.

这是代码....

<Application x:Class="SplashScreenDemo.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Splash.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)

app代码背后......

internal delegate void Invoker();
public partial class App : Application
{
    public App()
    {
        ApplicationInitialize = _applicationInitialize;
    }
    public static new App Current
    {
        get { return Application.Current as App; }
    }
    internal delegate void ApplicationInitializeDelegate(Splash splashWindow);
    internal ApplicationInitializeDelegate ApplicationInitialize;
    private void _applicationInitialize(Splash splashWindow)
    {
        // fake workload, but with progress updates.
        Thread.Sleep(500);
        splashWindow.SetProgress(0.2);

        Thread.Sleep(500);
        splashWindow.SetProgress(0.4);

        Thread.Sleep(500);
        splashWindow.SetProgress(0.6);

        Thread.Sleep(500);
        splashWindow.SetProgress(0.8);

        Thread.Sleep(500);
        splashWindow.SetProgress(1);

        // Create the main window, but on the UI thread.
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate
        {
            MainWindow = new Window1();
            MainWindow.Show();
        });           
    }
}
Run Code Online (Sandbox Code Playgroud)

飞溅xaml(实际上,这里没有什么太有趣......)

<Window x:Class="SplashScreenDemo.Splash"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Splash" Height="300" Width="300">
    <Grid>
        <TextBlock Height="21" Margin="91,61,108,0" VerticalAlignment="Top">Splash Screen</TextBlock>
        <ProgressBar Name="progBar" Margin="22,122,16,109" Minimum="0" Maximum="1"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

启动代码隐藏...

public partial class Splash : Window
{
    public Splash()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Splash_Loaded);
    }

    void Splash_Loaded(object sender, RoutedEventArgs e)
    {
        IAsyncResult result = null;

        // This is an anonymous delegate that will be called when the initialization has COMPLETED
        AsyncCallback initCompleted = delegate(IAsyncResult ar)
        {
            App.Current.ApplicationInitialize.EndInvoke(result);

            // Ensure we call close on the UI Thread.
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate { Close(); });
        };

        // This starts the initialization process on the Application
        result = App.Current.ApplicationInitialize.BeginInvoke(this, initCompleted, null);
    }

    public void SetProgress(double progress)
    {
        // Ensure we update on the UI Thread.
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate { progBar.Value = progress; });           
    }
}
Run Code Online (Sandbox Code Playgroud)

当工作在工作线程上完成时,进度条将很好地更新,并且您在启动屏幕上的任何动画将保持娱乐滚动.

  • 一个以下划线开头的方法声明......真的吗? (4认同)
  • (调用者)对我不起作用,但是用“ new Action(delegate()”)代替了 (2认同)