wpf异步加载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Data;
using System.ComponentModel;
using System.Windows.Threading;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
// static string fileName = @"C:\MyLearn\eqrms_hk_20111219_listedposn_ff\EQRMS_HK_20111219_EXO.csv";
static string fileName = @"C:\Users\admin\Desktop\个人库数据导入模版.xlsx";
DataTable dt = new DataTable("Info");
delegate void BindDataAsync();
GridView gv = new GridView();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
#region Invoke the Binding Data method asynchorously
private void BeginBindData()
{
BindDataAsync bindData = new BindDataAsync(BindData);
IAsyncResult iar = bindData.BeginInvoke(new AsyncCallback(EndBindData), bindData);
tsStatus.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
tsStatus.Content = "正在加载中。。。";
}));
}
private void EndBindData(IAsyncResult iar)
{
tsStatus.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
tsStatus.Content = "加载完毕。。。";
}));
BindDataAsync bindData = (BindDataAsync)iar.AsyncState;
bindData.EndInvoke(iar);
}
private void BindData()
{
GetHeader();
DataTable dt = LoadData();
tsStatus.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
tsStatus.Content = "正在绑定数据,请稍候。。。";
}));
//using Dispatcher to avoid crossing thread exception
listView1.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
foreach (DataColumn item in dt.Columns)
{
GridViewColumn gvc = new GridViewColumn();
gvc.DisplayMemberBinding = new Binding(item.ColumnName);
gvc.Header = item.ColumnName;
gv.Columns.Add(gvc);
}
listView1.View = gv;
listView1.DataContext = dt;
listView1.SetBinding(ListView.ItemsSourceProperty, new Binding()); //must add this , otherwise it will not be showen
}));
}
#endregion
#region Invoke the Coloring method asynchorously
private void InsertRowNumber()
{
DataTable dtRowNumber = new DataTable();
DataColumn dcRowNum = new DataColumn("RowNum",typeof(string));
dtRowNumber.Columns.Add(dcRowNum);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dtRowNumber.NewRow();
dr[0] = i.ToString();
dtRowNumber.Rows.Add(dr);
}
GridViewColumn columnRowNum = new GridViewColumn();
foreach(DataColumn dc in dtRowNumber.Columns)
{
columnRowNum.DisplayMemberBinding = new Binding(dc.ColumnName);
columnRowNum.Header = dc.ColumnName;
gv.Columns.Insert(0, columnRowNum);
}
listView1.View = gv;
listView1.DataContext = dtRowNumber;
listView1.SetBinding(ListView.ItemsSourceProperty,new Binding());
}
#endregion
#region Initialize the DataTable
private void GetHeader()
{
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{
string result = sr.ReadLine();
if (!String.IsNullOrEmpty(result))
{
string[] _result = result.Split(',');
if (_result.Length != 0)
{
for (int i = 0; i < _result.Length; i++)
{
DataColumn dc = new DataColumn();
dc.ColumnName = _result[i];
dt.Columns.Add(dc);
}
}
}
}
}
private DataTable LoadData()
{
int tryResult;
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{
string result;
long count = 0;
while ((result = sr.ReadLine()) != null)
{
string[] _result = result.Split(',');
if (Int32.TryParse(_result[0], out tryResult))
{
DataRow dr = dt.NewRow();
for (int m=0;m<_result.Length;m++)
{
dr[m] = _result[m];
}
count++;
tsStatus.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
tsStatus.Content = "当前已经加载"+count+"条数据。。。";
}));
dt.Rows.Add(dr);
}
}
}
return dt;
}
#endregion
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
BeginBindData();
}
private void btnAddRowNum_Click(object sender, RoutedEventArgs e)
{
InsertRowNumber();
}
}
}