WINUI3中调用GetThumbnailAsync(ThumbnailMode.PicturesView)返回空值/空引用异常的问题求助
WINUI3中调用GetThumbnailAsync(ThumbnailMode.PicturesView)返回空值/空引用异常的问题求助
各位大佬好!我刚从C# WebApp转过来做WINUI3的Windows应用,用的是VS2022。现在跟着Microsoft Learn的Simple Photo Editor示例复刻,但遇到了个小问题,卡了好一会儿了,麻烦大家给点思路~
具体情况是这样的:我调用await ImageFile.GetThumbnailAsync(ThumbnailMode.PicturesView)时,返回的thumbnail一直是空值。但换成ThumbnailMode.DocumentsView就能正常拿到低分辨率的缩略图,另外用GetImageSourceAsync加载完整图片也完全没问题,这就很困惑,我到底哪里没做对?
下面是我的相关代码:
ImageFileInfo.cs
public class ImageFileInfo : INotifyPropertyChanged { public ImageFileInfo(ImageProperties properties, StorageFile imageFile, string name, string type) { ImageProperties = properties; ImageName = name; ImageFileType = type; ImageFile = imageFile; var rating = (int)properties.Rating; var random = new Random(); ImageRating = rating == 0 ? random.Next(1, 5) : rating; } public StorageFile ImageFile { get; } public ImageProperties ImageProperties { get; } public async Task<BitmapImage> GetImageSourceAsync() { using (IRandomAccessStream fileStream = await ImageFile.OpenReadAsync()) { //create a bitmap to be the image source BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(fileStream); return bitmapImage; } } public async Task<BitmapImage> GetImageThumbnailAsync() { var thumbnail = await ImageFile.GetThumbnailAsync(ThumbnailMode.PicturesView); //ImageFile.GetThumbnailAsync(ThumbnailMode.PicturesView); // Create a bitmap to be the image source BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(thumbnail); thumbnail.Dispose(); return bitmapImage; } }
MainWindow.xaml.cs
public sealed partial class MainWindow : Window { public ObservableCollection<ImageFileInfo> Images { get; }= new ObservableCollection<ImageFileInfo>(); public MainWindow() { this.InitializeComponent(); GetItemsAsync(); } private async Task GetItemsAsync() { StorageFolder appInstalledFolder = Package.Current.InstalledLocation; StorageFolder picturesFolder = await appInstalledFolder.GetFolderAsync("Assets\\Samples"); var result = picturesFolder.CreateFileQueryWithOptions(new QueryOptions()); IReadOnlyList<StorageFile> imageFiles = await result.GetFilesAsync(); foreach(StorageFile file in imageFiles) { Images.Add(await LoadImageInfoAsync(file)); } ImageGridView.ItemsSource = Images; } public async static Task<ImageFileInfo> LoadImageInfoAsync(StorageFile file) { var properties = await file.Properties.GetImagePropertiesAsync(); ImageFileInfo info = new(properties, file, file.DisplayName, file.DisplayType); return info; } private void ImageGridView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args) { if (args.InRecycleQueue) { var templateRoot = args.ItemContainer.ContentTemplateRoot as Grid; var image = templateRoot.FindName("ItemImage") as Image; image.Source = null; } if(args.Phase == 0) { args.RegisterUpdateCallback(ShowImage); args.Handled = true; } } private async void ShowImage(ListViewBase sender, ContainerContentChangingEventArgs args) { if(args.Phase == 1) { var templateRoot = args.ItemContainer.ContentTemplateRoot as Grid; var image = templateRoot.FindName("ItemImage") as Image; var item = args.Item as ImageFileInfo; image.Source = await item.GetImageThumbnailAsync(); } } }
备注:内容来源于stack exchange,提问作者Charles de M.
相关产品推荐
相关产品推荐

