我发现这一点:How to show a loading graphic/animation when wpf data binding is taking place加载动画在新窗口时WPF数据网格加载
但不知道如何运用它为我的工作。
我有我的主窗口。它会调用我所有的用户控件。
在用户控制,我有一个DataGrid。按“Go”按钮后,DataGrid会从MySQL加载数据。这需要很长时间,我想在加载数据生成过程中显示一个对话窗口,其中包含“Please Wait”。
我发现下面的链接,但不understant如何正确地调用它。
我需要把这个“装载”,在一个新的类文件,如“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;
}
}
}
}
但不要错误的工作:
,因为不同的线程拥有它
调用线程不能访问该对象,我知道它柯朗错误,但我不“看不到‘DispatcherTimer’要不,我已经在先例项目见过......所以我会尽力的第二码明天BUT。我不明白的地方,我把我的方法:(
EDIT 2
我用你的代码试图......我太stupide说不定。
我已经写了类一个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 = "";
}
}
是你能运用它为您的工作? –
对不起,我在阻止其他问题,我还没有开始这项任务。我明天开始吧:)然后回到这里。 – Saesee