I'm creating a photo gallery app and I want to show pictures by category.
In normal mode, everything works well and the images are displayed.
But when the number of images gets bigger (300), the program will hang and it takes a long time to show.
So I want to use asynchronous and display images.
I used the following code but nothing happens and the images are not displayed
int HandleFileAsync()
{
AllofItems.ForEachWithIndex((item, idx) =>
{
var cv = new CoverViewItem();
var contentImg = new Image();
contentImg.Stretch = Stretch.UniformToFill;
contentImg.Source = new BitmapImage(new Uri(item, UriKind.Absolute));
var img = new Image();
img.Source = new BitmapImage(new Uri(item, UriKind.Absolute));
//-< source >-
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(item, UriKind.Absolute);
//< thumbnail >
src.DecodePixelWidth = 160;
src.CacheOption = BitmapCacheOption.OnLoad;
//</ thumbnail >
src.EndInit();
img.Source = src;
//-</ source >-
img.Stretch = Stretch.Uniform;
img.Height = 160;
cv.Header = img;
cv.Tag = item;
cv.Content = contentImg;
cv.Selected += Cv_Selected;
cv.Deselected += Cv_Deselected;
Dispatcher.Invoke(() =>
{
cover.Items.Add(cv);
});
});
return AllofItems.Count();
}
async void Example()
{
// This method runs asynchronously.
int t = await Task.Run(() => HandleFileAsync());
Console.WriteLine("Compute: " + t);
}
private void Listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
AllofItems = GetFileList(@"E:\DL\newArtWork\Art\" + listbox.SelectedItem).ToArray();
cover.Items.Clear();
Example();
}