none
WPF 异步窗体加载等待 RRS feed

  • 问题

  • WPF中 做了个Main窗体 由于窗体 数据量大 每次打开的时候都有假死的现象,我想做个异步等待的窗体,启动的时候先弹出Load窗体,在Main窗体完全加载好了再显示Main窗体  如何实现?
    2014年9月2日 2:12

答案

  • 楼主 你好,

    很抱歉一开始理解错你的意图。我创建了一个例子来实现这个需求。首先将APP.XAML中开始窗体 (StartupUri) 删除掉,然后再APP.XAML.CS中添加下面代码,

            BackgroundWorker backgroundWorker = new BackgroundWorker();
            InitailWindow initail = new InitailWindow();
            MainWindow main = null;
            
            public App()
            {
                backgroundWorker.DoWork += backgroundWorker_DoWork;
                backgroundWorker.WorkerReportsProgress = true;
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
                backgroundWorker.RunWorkerAsync();
            }
    
            void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                this.Dispatcher.BeginInvoke(new Action(() => 
                {
                    main = new MainWindow();
                    initail.Hide();
                    main.Show();
                }));
            }
    
            void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                this.Dispatcher.BeginInvoke(new Action(() => { initail.Show(); }));
               
                //Do a lot of work
                for(int i =0; i<5; i++)
                {
                    Thread.Sleep(1000);
                }
            }

    XAML:

    <Window x:Class="WPFSplashScreen.InitailWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="InitailWindow" Height="100" Width="300" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
        <Grid>
            <ProgressBar x:Name="progressBar" IsIndeterminate="True" Width="280" Height="30"/>
        </Grid>
    </Window>

    我把例子上传至OneDrive中,可以从这里下载。

    另外推荐一些资料:

    http://code.msdn.microsoft.com/windowsapps/Dynamic-Flash-Screen-in-WPF-6b1f1dc4

    http://stackoverflow.com/questions/22026209/wpf-splashscreen-with-progressbar

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.=

    2014年9月3日 2:08
    版主

全部回复

  • 楼主 你好,

    请参考下面的代码:

            public MainWindow()
            {
                InitializeComponent();
                this.Loaded += MainWindow_Loaded;
            }
    
            void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                ThreadPool.QueueUserWorkItem((x) => {
                    //处理数据
                    //同步到UI
                    this.Dispatcher.BeginInvoke(new Action(() => {
                        
                    }));
                });
    
            }
    谢谢!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月2日 6:22
    版主
  • 你好! 我的意思是现在有两个窗体  Initwin 和 mainWin。  Initwin 就是个想进度条一样的等待窗体,启动程序的时候,Initwin 和 mainWin 一起启动,Initwin 监听mainWin ,等mainWin 窗体加载完全了 再显示出来
    2014年9月2日 6:47
  • 楼主 你好,

    很抱歉一开始理解错你的意图。我创建了一个例子来实现这个需求。首先将APP.XAML中开始窗体 (StartupUri) 删除掉,然后再APP.XAML.CS中添加下面代码,

            BackgroundWorker backgroundWorker = new BackgroundWorker();
            InitailWindow initail = new InitailWindow();
            MainWindow main = null;
            
            public App()
            {
                backgroundWorker.DoWork += backgroundWorker_DoWork;
                backgroundWorker.WorkerReportsProgress = true;
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
                backgroundWorker.RunWorkerAsync();
            }
    
            void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                this.Dispatcher.BeginInvoke(new Action(() => 
                {
                    main = new MainWindow();
                    initail.Hide();
                    main.Show();
                }));
            }
    
            void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                this.Dispatcher.BeginInvoke(new Action(() => { initail.Show(); }));
               
                //Do a lot of work
                for(int i =0; i<5; i++)
                {
                    Thread.Sleep(1000);
                }
            }

    XAML:

    <Window x:Class="WPFSplashScreen.InitailWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="InitailWindow" Height="100" Width="300" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
        <Grid>
            <ProgressBar x:Name="progressBar" IsIndeterminate="True" Width="280" Height="30"/>
        </Grid>
    </Window>

    我把例子上传至OneDrive中,可以从这里下载。

    另外推荐一些资料:

    http://code.msdn.microsoft.com/windowsapps/Dynamic-Flash-Screen-in-WPF-6b1f1dc4

    http://stackoverflow.com/questions/22026209/wpf-splashscreen-with-progressbar

    谢谢!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.=

    2014年9月3日 2:08
    版主