5

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

4

3 回答 3

6

您应该查看这篇关于在 WPF中创建SplashScreen的 MSDN 文章。本质上,您将要显示的图像添加到项目中,并将构建操作设置为 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();
    }
}
于 2012-12-12T08:50:50.183 回答
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();
}

还要寻找显示加载屏幕的最佳方式。您也可以在主窗口中显示一些动画。我不认为显示表格是最好的方法。

于 2012-12-12T08:48:09.767 回答
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
}
于 2012-12-12T08:47:49.330 回答