Home > Net >  WPF listview one-time loading a lot of slow listviewitem node, what method to speed up the load node
WPF listview one-time loading a lot of slow listviewitem node, what method to speed up the load node

Time:09-20

From learning c # to now, I make up the performance of the program is very fast but today with the listview simulation file browsing interface, click the button to load address stored in an array or a collection of files, the last for 10 seconds or so within the listview control display, why? This how to solve ah, sometimes even runtime throws
"the state has for 60 seconds, with the target context of threads/cell is likely to perform pump waiting for or in the case of no Windows messages sent to deal with a running time is very long, this usually affects performance, may even lead to the application does not respond or use memory constantly accumulated over time, to avoid this problem, all the single thread unit pump should be used (STA) thread is waiting for primitives (like CoWaitForMultipleHandles), and in the process of the long-running operation regularly sends the message," abnormal

Why do most of the file browser software, click on a folder node, has just finished loading? Is it in the cache? I am just learning WPF don't have to be new, to simulate file browsing interface, compile results slow more than 10 seconds, what is the solution, optimized? Speed up the execution of code? Or make a paging? Or use asynchronous programming load (thread I haven't learned, you can consider it if short learning time to finish it), can say in what way is better, paging, speed up the execution of code, asynchronous programming load or other methods (preferably not upgrade computer configuration),

Code

//click this button to load a one-time loading large listviewitem node, the node corresponding to a picture
Private void button_Click_1 (object sender, RoutedEventArgs e)
{

DirectoryInfo TheFolder=new DirectoryInfo (@ "scenery pictures E: \ ");
String a=a DateTime. Now. The ToString ();
Foreach (the FileInfo NextFile in TheFolder. GetFiles ())
{

If (System. IO. Path. GetExtension (NextFile. FullName)==". BMP "| | System. IO. Path. GetExtension (NextFile. FullName)==" JPG "
| | System. IO. Path. GetExtension (NextFile. FullName)=="PNG")
{
ListViewItem b=new ListViewItem ();
B.W idth=120;
Thickness thick=new Thickness (10);
B.M argin=thick;
Listview1. Items. The Add (b);
The StackPanel sp=new the StackPanel ();
Biggest ontent=sp;
Sp. HorizontalAlignment=HorizontalAlignment. Center;
Sp. VerticalAlignment=VerticalAlignment. Center;
Image ig=new Image ();

Ig. Source=new BitmapImage (new Uri (NextFile. FullName));
Ig. Stretch=Stretch. Uniform;
Ig. Height=90;

TextBlock TB=new TextBlock ();
TB. Text=System. IO. Path. GetFileNameWithoutExtension (NextFile. Name);
TB. Height=50;
Sp. Children. The Add (ig);
Sp. Children. The Add (TB);
}

}



}

CodePudding user response:

Listbox and listview are not suitable for present large amounts of data control, almost no optimization, paging, or use the GridView, rewrite the template, more easily than listview control

CodePudding user response:

page

CodePudding user response:

Lazy loading, can like mobile phone brush weibo, first part load, such as drag down when loading part, or add a "load more" at the bottom button, click to load more content, such an approach to estimate your code changes will be relatively small,

CodePudding user response:

reference 1st floor rocmemory response:
listbox and listview are not suitable for present large amounts of data control, almost no optimization, paging, or use the GridView, rewrite the template, more easily than listview control
, that have the source code, please? How do you write or should I? I've never used the gridview or through the release of the waste, empty memory solution?

CodePudding user response:

Because did not add the clear?

CodePudding user response:

Suffered most is to see you write in the form of winform WPF data binding him not sweet? Why you always think you have to go to the WPF slowly understand!
First the xaml code:
 & lt; Grid> 







In the back-end code is:
 public partial class MainWindow: Window 
{
Private ObservableCollection ImgInfos=new ObservableCollection (a);
Public MainWindow ()
{
InitializeComponent ();
LvwTest. ItemsSource=ImgInfos;
}

Private void Window_KeyDown (object sender, KeyEventArgs e)
{
If (e.K ey==Key. Left)
{
ImgInfos. The Clear ();
Var files=System. IO. Directory. GetFiles (" D:/image ");
The foreach (var file in files)
{
ImgInfos. Add (new ImgInfo ()
{
FileName=System. IO. Path. GetFileNameWithoutExtension (file)
,
ImgSource=new BitmapImage (new Uri (file))
});
}
}
If (e.K ey==Key. Right)
{
ImgInfos. The Clear ();
}
}
}
Public class ImgInfo
{
Public BitmapImage ImgSource {get; set; }
Public string FileName {get; set; }
}

Finally run results right click add empty Jane simple single would be less than one hundred lines of code efficiency can improve a lot of times:

CodePudding user response:

If you think that will be added CARDS can also be set piece with asynchronous delegate, so at least interface does not like what you said stuck:
 foreach (var file in files) 
{
The Dispatcher. The BeginInvoke (()=& gt; ImgInfos. Add (new ImgInfo ()
{
FileName=System. IO. Path. GetFileNameWithoutExtension (file)
,
ImgSource=new BitmapImage (new Uri (file))
}));
}

Pay attention to the reference namespace:
 using System. Windows. When; 

CodePudding user response:

And filter file that piece don't use many times IO operations performance under very directly by judging the string have to do is to screen for example:
 
Var files=System. IO. Directory. GetFiles (" D:/image ");
Files=files. Where (s=& gt; S.E ndsWith (" PNG ", StringComparison. OrdinalIgnoreCase) |
S.E ndsWith (". BMP ", StringComparison. OrdinalIgnoreCase) |
nullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related