显示加载窗口

时间:2012-12-12 08:38:45

标签: c# .net wpf window loading

我在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()方法中加载所有应用程序外部资源,而不是在主类方法中加载。

3 个答案:

答案 0 :(得分:5)

您应该查看有关在WPF中创建SplashScreen的MSDN文章。基本上,您将要显示的图像添加到项目中,并将Build Action设置为SplashScreen,它将在程序启动时显示,并在显示主应用程序窗口时消失。


您还可以尝试导入System.ComponentModel类并使用BackgroundWorker来显示您的加载表单,它将允许您保留UI的响应能力。

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();
    }
}

答案 1 :(得分:2)

您应该将耗费时间的代码放在后台线程中(为此您可以使用BackgroundWorker,Task或Async Await,具体取决于您的dot net框架版本)

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();
}

另外,请注意显示加载屏幕的最佳方式。您也可以在主窗口中显示一些动画。我不认为展示表格是最好的方式。

答案 2 :(得分:1)

我前一段时间你做过一个Loader课程你可以使用。它在执行加载方法时显示一个窗口,在完成时关闭它并为您提供方法的输出:

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();
    }
}

如何使用它:

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
}