1

我发现了这个:当 wpf 数据绑定发生时如何显示加载图形/动画

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

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

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

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

我需要把这个“加载器”放在像“Loader.cs”这样的新类文件中,然后按钮调用它吗?好的,但是当数据网格完成时如何关闭它?

我迷路了......好吧,如果存在其他解决方案并且只需使用......

提前致谢

编辑测试 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 = "";
    }
}
4

1 回答 1

2

您将以下代码放在 DataGrid-UserControl 代码隐藏的方法中,该方法在您的按钮单击事件处理程序中调用:

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
}
于 2012-10-18T12:37:18.853 回答