語境
我正在嘗試將用戶本地圖片檔案夾(例如 C:\Users\Username\Pictures\The Folder)內的目錄中的影像顯示到 GridView 中。
我正在為影像使用一個類(這是 GridView 的源代碼):
public class SpotlightImage
{
public string ID { get; set; }
public string Name { get; set; }
public string FileSource { get; set; }
}
GridView 的 XAML 如下:
<GridView
Padding="{StaticResource MediumLeftRightMargin}"
animations:Connected.ListItemElementName="thumbnailImage"
animations:Connected.ListItemKey="galleryAnimationKey"
IsItemClickEnabled="True"
ItemClick="ImagesGridView_ItemClick"
ItemsSource="{x:Bind Source, Mode=OneWay}"
RightTapped="ImagesGridView_RightTapped"
SelectionMode="None">
<GridView.ItemTemplate>
<DataTemplate x:DataType="models:SpotlightImage">
<Image
x:Name="thumbnailImage"
AutomationProperties.Name="{x:Bind Name}"
Source="{x:Bind FileSource}"
Style="{StaticResource ThumbnailImageStyle}"
ToolTipService.ToolTip="{x:Bind Name}" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
用于填充源代碼的 C# 如下(我意識到很可能不需要使用并行 foreach,但是,我使用它是為了與專案中的其他代碼保持一致):
private async void ImagesPage_OnLoaded(object sender, RoutedEventArgs e)
{
Source.Clear();
storageFiles = new List<Windows.Storage.StorageFile>();
List<string> SavedWallpapers = await RetrieveAllSavedWallpapers.Main(); //Retrieves image names
galleryData = new List<SpotlightImage>();
Parallel.ForEach(SavedWallpapers, spotlightFile =>
{
imgFileFunction(spotlightFile);
});
async void imgFileFunction(string spotlightFile)
{
try
{
//StorageReferences.WallpaperSaveStorage is a StorageFolder variable for the images directory being accessed
Windows.Storage.StorageFile file = await StorageReferences.WallpaperSaveStorage.GetFileAsync(spotlightFile);
storageFiles.Add(file);
var img = new SpotlightImage()
{
ID = file.DisplayName,
FileSource = file.Path,
Name = file.DisplayName
};
galleryData.Add(img);
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
Source.Add(img);
Debug.WriteLine("IMAGE ADDED: " img.Name);
}
);
}
catch (Exception ex)
{
Debug.WriteLine("MULTITHREADED ERROR: " ex.Message);
}
}
}
問題
GridView 顯示空方塊而不是影像。出現正確數量的“影像”,但它們是空白的。
我試過的
我查看了 StackOverflow 上的類似問題并嘗試了以下方法:
- 使用 UriSource 而不是源(同樣的問題仍然存在)
- 查看代碼(我在專案的其他地方使用了完全相同的代碼,它在那里作業。唯一的區別是正在訪問不同的檔案夾)
- 檢查權限(我在之前的代碼中使用了這個確切的目錄,沒有問題)
筆記:
我的 UWP 應用程式已經具有廣泛的檔案系統訪問(這對于我的應用程式的目的是必要的)。
我有一個 FlipView,它正在訪問相同的檔案并遇到完全相同的問題。
我已經在我的專案的其他地方完美地運行了完全相同的代碼,唯一的區別是正在訪問的檔案夾。這讓我相信問題出在權限上,但我的應用程式具有廣泛的檔案系統訪問權限。所以這不應該是問題(?)
您可能會說,我對 UWP 比較陌生。因此,如果有一些明顯的權限問題或類似問題,請對我放輕松:)
uj5u.com熱心網友回復:
基于
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/461935.html
上一篇:Xamarin.Forms不同設備上同一應用程式內的兩個頁面之間的通信
下一篇:過濾串列時WPF松散系結
