我加載一個文本的大數據文件並將其顯示在數據網格中, 問題是,窗口很慢並且不平滑, 我該如何實現下面的代碼更好?使Wpf更加平滑
按鈕代碼:
private async void MILoadLogFile_Click(object sender, RoutedEventArgs e) {
// Configure open file dialog box
OpenFileDialog oFD = new OpenFileDialog();
// Did they click on the OK button?
if (oFD.ShowDialog() == true) {
await myLogSession.LoadfromFileAsync(oFD.FileName);
}
}
的LOCAD方法:(抱歉長碼)
public async Task LoadfromFileAsync(String fileName) {
compassLogCollection.Clear();
StreamReader streamReader = new StreamReader(fileName);
if (fileName.Contains("Compass")) {
String temp = "";
String line;
DateTime dateTime = new DateTime();
LoggingLvl loggingLvl = new LoggingLvl();
LoggingLvl.ELoggingLvl eLoggingLvl = new LoggingLvl.ELoggingLvl();
char[] delimiters = new[] {' '};
string threadId = "";
string loggingMessage;
string dateAndTimestamp = "";
int ff = 0;
try {
using (streamReader) {
while ((line = await streamReader.ReadLineAsync()) != null) {
//while ((line = streamReader.ReadLine()) != null) {
string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
foreach (string t in parts) {
switch (ff) {
case 0:
dateAndTimestamp = t;
break;
case 1:
dateAndTimestamp += " " + t.Replace(",", ".");
dateTime = DateTime.Parse(dateAndTimestamp);
dateAndTimestamp = "";
break;
case 2:
eLoggingLvl = loggingLvl.ParseLoggingLvl(t);
break;
case 3:
threadId = t;
break;
default:
temp += t;
break;
}
ff++;
}
loggingMessage = temp;
temp = "";
ff = 0;
loggingLvl = new LoggingLvl(eLoggingLvl);
CompassLogData cLD = new CompassLogData(dateTime, loggingLvl, threadId, loggingMessage);
compassLogCollection.Add(cLD);
}
Console.Out.WriteLine("DOOOOOOOOOOOOONE");
}
} catch (Exception e) {
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
顯示你綁定到UI的位置 – Paparazzi