加载WPF数据网格时在新窗口中加载动画

时间:2012-10-18 12:21:43

标签: c# wpf multithreading user-controls loading

我找到了这个:How to show a loading graphic/animation when wpf data binding is taking place

但不明白如何将它应用于我的工作。

我有我的主窗口。它会调用我的所有用户控件。

在用户控件上,我有一个DataGrid。按“Go”按钮后,datagrid从MySQL加载数据。这需要很长时间,我想在加载datagrig期间显示一个带有“Please Wait”的对话窗口。

我找到了以下链接,但不明白如何正确调用它。

我是否需要将此“加载器”放在像“Loader.cs”这样的新类文件中并按下它?好的,但是在datagrid完成时如何关闭它?

我迷路了......好吧,如果存在另一种解决方案,只需使用......

先谢谢

编辑测试1:

尝试使用滑块进行简单测试以获得等待时间和按钮。

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace TEST_LoadingPage
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnTester_Click(object sender, RoutedEventArgs e)
    {
        Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
        waitWindow.Content = new TextBlock { Text = "Veuillez patienter...", FontSize = 30, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };

        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += delegate
        {
            Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
            lbReussite.Visibility = Loop.Pause(slider.Value);
            Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
        };
        worker.RunWorkerAsync();
    }
}

public class Loop
{
    public static System.Windows.Visibility Pause(double duree)
    {
        try
        {
            DateTime begin = new DateTime();
            DateTime end = new DateTime();
            int i = 0;
            begin = DateTime.Now;
            end = DateTime.Now.AddSeconds(duree);

            while (DateTime.Now <= end)
                i++;

            return System.Windows.Visibility.Visible;
        }
        catch (Exception)
        {
            return System.Windows.Visibility.Hidden;
        }
    }
}
}

但不能处理错误:

  

调用线程无法访问此对象,因为另一个线程拥有它

我知道它有一个courant错误,但我没有看到“DispatcherTimer”或者我之前见过的先例项目...所以我明天会尝试第二个代码但是。我不明白我把方法放在哪里:(

编辑2

我尝试了你的代码......我太愚蠢了。

我在Loader.cs和我的MainWiondow中编写了这个类(它是一个测试)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnTester_Click(object sender, RoutedEventArgs e)
    {
        Loader<List<Donnees>> loader = new Loader<List<Donnees>>(GenerateList((int)slider.Value);
        loader.JobFinished += new Loader<Donnees>.OnJobFinished(loader_JobFinished);
        loader.Load();

    }

    private List<Donnees> GenerateList(int number)
    {
        List<Donnees> list = new List<Donnees>();
        for (int i = 1; i <= number; i++)
        {
            Donnees data = new Donnees();
            data.Id = i;
            data.Name = "Ligne " + i;
            list.Add(data);
        }
        return list;
    }

    void loader_JobFinished(object sender, List<Donnees> result)
    {
        result = GenerateList((int)slider.Value);
        dgDataGrid.ItemsSource = result;
    }

}

public class Donnees
{
    #region Properties

    private int id;
    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    #endregion

    public Donnees()
    {
        id = -1;
        name = "";
    }
}

1 个答案:

答案 0 :(得分:2)

您将以下代码放在DataGrid-UserControl的代码隐藏中的方法中,该方法在您的button-click-eventhandler中调用:

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

现在你可以决定......

...如果要创建一个DataLoader类,它在完成时加载数据并触发DataLoaded事件,而不是添加:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
    Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
    DataLoader dataLoader = new DataLoader();
    dataLoader.DataLoaded += delegate
    {
        Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
    };

    dataLoader.LoadData();
};

worker.RunWorkerAsync();

...或者如果您只是将数据加载代码复制到此方法中并添加:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
    Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
    //do dataloading here
    Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
};
worker.RunWorkerAsync();
编辑:我写了一个类,它应该在代码隐藏中没有太多代码的情况下做你想做的事情:

public class Loader<TFuncResult,TFirstArgType>:FrameworkElement
{
    private Func<TFirstArgType,TFuncResult> _execute;
    public TFuncResult Result { get; private set; }

    public delegate void OnJobFinished(object sender, TFuncResult result);
    public event OnJobFinished JobFinished;

    public Loader(Func<TFirstArgType,TFuncResult> 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(TFirstArgType firstarg, Window waitWindow = null)
    {
        if (waitWindow == null)
        {
            waitWindow = GetWaitWindow();
        }
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += delegate
        {
            Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
            Result = _execute(firstarg);
            Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
        };
        worker.RunWorkerCompleted += delegate
        {
            worker.Dispose();
            if (JobFinished != null)
            {
                JobFinished(this, Result);
            }
        };
        worker.RunWorkerAsync();
    }
}

编辑2:如何使用它:

假设您的GenerateList()返回类型为List<Donnees>的数据,且类型为int类型的参数(适用于所有类型):

将其放在要加载数据的位置(例如,在Window_Loaded-Event中):

Loader<List<Donnees>, int> loader = new Loader<List<Donnees>, int>(GenerateList);
loader.JobFinished += new Loader<List<Donnees>, int>.OnJobFinished(loader_JobFinished);
loader.Load((int)slider.Value);

现在在Code-Behind中调用此EventHandler:

void loader_JobFinished(object sender, List<Donnees> result)
{
    YourDataGrid.DataSource = result
}