积极答复者
WPF 异步窗体加载等待

问题
答案
-
楼主 你好,
很抱歉一开始理解错你的意图。我创建了一个例子来实现这个需求。首先将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.=- 已编辑 Leo (Apple) YangModerator 2014年9月3日 2:18 Edit
- 已标记为答案 Leo (Apple) YangModerator 2014年9月8日 2:10
全部回复
-
楼主 你好,
请参考下面的代码:
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. -
楼主 你好,
很抱歉一开始理解错你的意图。我创建了一个例子来实现这个需求。首先将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.=- 已编辑 Leo (Apple) YangModerator 2014年9月3日 2:18 Edit
- 已标记为答案 Leo (Apple) YangModerator 2014年9月8日 2:10