我正在使用AdaptiveGridView將圖像列表從文件夾加載到頁面上。我的問題是,如何在圖像準備好後立即加載圖像,而不必等待圖像的整個列表進行處理。 這裏是我的代碼: C#如何從UWP中的文件夾加載圖像到頁面時立即加載圖像
public class Images
{
public ImageSource ImageURL { get; set; }
public string ImageText { get; set; }
}
private List<Images> ImageCollection;
private async void Button_Click(object sender, RoutedEventArgs e)
{
ImageCollection = new List<Images>();
// pick a folder
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.FileTypeFilter.Add(".jpg");
var folder = await folderPicker.PickSingleFolderAsync();
var filesList = await folder.CreateFileQueryWithOptions(new QueryOptions(CommonFileQuery.DefaultQuery, new string[] { ".jpg", ".png", ".jpeg" })).GetFilesAsync();
for (int i = 0; i < filesList.Count; i++)
{
StorageFile imagefile = filesList[i];
BitmapImage bitmapimage = new BitmapImage();
using (IRandomAccessStream stream = await imagefile.OpenAsync(FileAccessMode.Read))
{
bitmapimage.SetSource(stream);
}
ImageCollection.Add(new Images()
{
ImageURL = bitmapimage,
ImageText = filesList[i].Name
});
countPhotos.Text = i.ToString() + "out of" + filesList.Count.ToString();
}
AdaptiveGV.ItemsSource = ImageCollection;
}
XAML:
<Page.Resources>
<DataTemplate x:Key="PhotosList">
<Grid>
<Image Source="{Binding ImageURL}"
Stretch="UniformToFill"
HorizontalAlignment="Center"
VerticalAlignment="Center">
</Image>
</Grid>
</DataTemplate>
</Page.Resources>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Content="click me" Click="Button_Click">
</Button>
<TextBlock Name="countPhotos">
</TextBlock>
<ScrollViewer>
<UWPToolkit:AdaptiveGridView x:Name="AdaptiveGV"
ItemHeight="300" DesiredWidth="500"
ItemTemplate="{StaticResource PhotosList}">
</UWPToolkit:AdaptiveGridView>
</ScrollViewer>
</StackPanel>
我累了移動的for循環,但裏面的AdaptiveGV.ItemsSource = ImageCollection;
放緩的過程中,我不認爲它要完成什麼是最好的方式我想在這裏做。任何其他建議? 謝謝
感謝soooooooo多<3 <3 <3 – user3159792