我在WPF应用程序加载外部资源,所以我想显示加载形式程序加载时。
我试图创建表单,并加载代码之前展示,并关闭加载时结束。
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadForm lf = new LoadForm();
lf.Visibility = Visibility.Visible;
// Al code that delays application loading
lf.Close();
}
但我得到的唯一的事情就是加载进度完成时,立即关闭形式显示。
我想,我需要使用的System.Threading,但不能确定。
谢谢。
注意我加载所有应用程序的外部资源Window_Loaded()方法,而不是在主类中的方法。
你应该看看这个MSDN文章在创造一个闪屏在WPF。 基本上你添加你想要展现给你的项目的形象,并设置生成操作闪屏会显示时,你的程序启动,当显示你的应用程序主窗口消失。
你也可以尝试导入System.ComponentModel
类,并使用BackgroundWorker的 ,以显示你的载表,它可以让你保留你的用户界面的响应。
public partial class MainWindow : Window
{
Window1 splash;
BackgroundWorker bg;
public MainWindow()
{
InitializeComponent();
bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
}
void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
splash.Hide();
}
void bg_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(10000);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
splash = new Window1();
splash.Show();
bg.RunWorkerAsync();
}
}
你应该把你的时间在后台线程消耗代码(可以使用的BackgroundWorker,任务或异步等待,这取决于你点.NET Framework版本)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadForm lf = new LoadForm();
lf.Visibility = Visibility.Visible;
//start the time consuming task in another thread
}
HeavyTaskCompleteEvent()
{
lf.Close();
}
也看出来,以显示加载屏幕的最佳途径。 您可以显示在主窗口中的一些动画以及。 我不认为呈现形式是最好的方式。
I made a Loader class a while ago you could use. It shows a Window while doing your loading-method, closes it when completed and gives you the output of the method:
public class Loader<TActionResult>:FrameworkElement
{
private Func<TActionResult> _execute;
public TActionResult Result { get; private set; }
public delegate void OnJobFinished(object sender, TActionResult result);
public event OnJobFinished JobFinished;
public Loader(Func<TActionResult> execute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
}
private Window GetWaitWindow()
{
Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
waitWindow.Content = new TextBlock { Text = "Please Wait", FontSize = 30, FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
return waitWindow;
}
public void Load(Window waitWindow = null)
{
if (waitWindow == null)
{
waitWindow = GetWaitWindow();
}
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
Result = _execute();
Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
};
worker.RunWorkerCompleted += delegate
{
worker.Dispose();
if (JobFinished != null)
{
JobFinished(this, Result);
}
};
worker.RunWorkerAsync();
}
}
How to use it:
Loader<TResult> loader = new Loader<TResult>(MethodName);
loader.JobFinished += new Loader<TResult>.OnJobFinished(loader_JobFinished);
loader.Load();
void loader_JobFinished(object sender, TResult result)
{
// do whatever you want with your result here
}