我是C#的新手,这是我自己完成的第一个真正的项目。现在,我面对这个挑战,我不知道如何克服。 以下是我到目前为止遇到的问题。 单击单选按钮后,WPF listView控件大约需要30秒才能显示该表。我被告知要提高它的性能,但这是我能想到的唯一解决方案。请给我一些关于如何缩短listView显示整个表的时间的建议。
提前致谢。 PS - 请原谅我的英语。这不是我的第一语言。
private void CarRadioButton_Checked(object sender, RoutedEventArgs e)
{
Prodave6.FieldType FType = Prodave6.FieldType.D;
UInt16 BlkNr = 100;
UInt32 pDatLen = 0;
UInt32 pAmount = 2;
UInt32 BufLen = 2;
#region New list Creation
List<Car> carList = new List<Car>();
int values;
//int[] startNumber = new int[16 * NumbersOfCars];
int[] startNumber = new int[16];
for (int i = 0; i < NumbersOfCars; i++)
{
byte[] pReadBuffer4 = new byte[2];
byte[] pReadBuffer5 = new byte[2];
byte[] pReadBuffer6 = new byte[2];
byte[] pReadBuffer7 = new byte[2];
byte[] pReadBuffer8 = new byte[2];
byte[] pReadBuffer9 = new byte[2];
byte[] pReadBuffer10 = new byte[2];
byte[] pReadBuffer11 = new byte[2];
byte[] pReadBuffer12 = new byte[2];
byte[] pReadBuffer13 = new byte[2];
byte[] pReadBuffer14 = new byte[2];
byte[] pReadBuffer15 = new byte[2];
byte[] pReadBuffer = new byte[4];
byte[] pReadBuffer1 = new byte[1];
byte[] pReadBuffer2 = new byte[1];
byte[] pReadBuffer3 = new byte[1];
for (int counter = 0; counter < 16; counter++)
{
switch (counter)
{
case 12:
startNumber[12] = 24 + i * 60;
pAmount = 4;
BufLen = 4;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer, ref pDatLen);
break;
case 13:
startNumber[13] = 28 + i * 60;
pAmount = 1;
BufLen = 1;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer1, ref pDatLen);
break;
case 14:
startNumber[14] = 29 + i * 60;
pAmount = 1;
BufLen = 1;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer2, ref pDatLen);
break;
case 15:
startNumber[15] = 29 + i * 60;
pAmount = 1;
BufLen = 1;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer3, ref pDatLen);
break;
case 0:
startNumber[0] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer4, ref pDatLen);
break;
case 1:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer5, ref pDatLen);
break;
case 2:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer6, ref pDatLen);
break;
case 3:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer7, ref pDatLen);
break;
case 4:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer8, ref pDatLen);
break;
case 5:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer9, ref pDatLen);
break;
case 6:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer10, ref pDatLen);
break;
case 7:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer11, ref pDatLen);
break;
case 8:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer12, ref pDatLen);
break;
case 9:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer13, ref pDatLen);
break;
case 10:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer14, ref pDatLen);
break;
case 11:
startNumber[counter] = counter * 2 + i * 60;
pAmount = 2;
BufLen = 2;
values = Prodave6.field_read_ex6(FType, BlkNr, startNumber[counter], pAmount, BufLen, pReadBuffer15, ref pDatLen);
break;
}
}
carList.Add(new Car()
{
Name = "小车" + (i + 1),
Step = Prodave6.bytes_2_word(pReadBuffer4[0], pReadBuffer4[1]),
Real_Target_Bin = Prodave6.bytes_2_word(pReadBuffer5[0], pReadBuffer5[1]),
Mark_Bin = Prodave6.bytes_2_word(pReadBuffer6[0], pReadBuffer6[1]),
Usable_Bin_1 = Prodave6.bytes_2_word(pReadBuffer7[0], pReadBuffer7[1]),
Usable_Bin_2 = Prodave6.bytes_2_word(pReadBuffer8[0], pReadBuffer8[1]),
Usable_Bin_3 = Prodave6.bytes_2_word(pReadBuffer9[0], pReadBuffer9[1]),
Usable_Bin_4 = Prodave6.bytes_2_word(pReadBuffer10[0], pReadBuffer10[1]),
Usable_Bin_5 = Prodave6.bytes_2_word(pReadBuffer11[0], pReadBuffer11[1]),
Package_Weight = Prodave6.bytes_2_word(pReadBuffer12[0], pReadBuffer12[1]),
Supply_Mode = Prodave6.bytes_2_word(pReadBuffer13[0], pReadBuffer13[1]),
Supply_From = Prodave6.bytes_2_word(pReadBuffer14[0], pReadBuffer14[1]),
Cycles = Prodave6.bytes_2_word(pReadBuffer15[0], pReadBuffer15[1]),
ParcelID = Prodave6.bytes_2_dword(pReadBuffer[0], pReadBuffer[1], pReadBuffer[2], pReadBuffer[3]),
Delay_Time = pReadBuffer1[0],
Big_Package = Prodave6.CheckBit1(pReadBuffer2[0]),
Locked = Prodave6.CheckBit2(pReadBuffer3[0])
});
}
myListView.ItemsSource = carList;
#endregion
}
答案 0 :(得分:-1)
这并不是严格意义上的改善,而是改善生活质量。您可以使用Background Worker在单独的线程上执行繁重的工作。这允许您保持主视图的启用(您没有锁定UI的那种尴尬感)。如果你的工作变得更重,你甚至可以添加一个进度条