gpt4 book ai didi

c# - Windows 8 - 如何正确获取嵌套文件夹?

转载 作者:可可西里 更新时间:2023-11-01 10:37:42 25 4
gpt4 key购买 nike

我有一个绑定(bind)到从磁盘加载图像的对象集合的 GridView。

对象在可见时被放入堆栈,图像按顺序从堆栈中加载。

问题是 GetFolderAsync() 在包含对象的 ScrollViewer 停止滚动之前不会返回。

代码如下:

    public static async Task<StorageFolder> GetFileFolderAsync(String fileUrl)
{
try
{
string filePathRelative = DownloadedFilePaths.GetRelativeFilePathFromUrl(fileUrl);
string[] words = filePathRelative.Split('\\');
StorageFolder currentFolder = await DownloadedFilePaths.GetAppDownloadsFolder();
for (int i = 0; (i < words.Length - 1); i++)
{
//this is where it "waits" for the scroll viewer to slow down/stop
currentFolder = await currentFolder.GetFolderAsync(words[i]);
}
return currentFolder;
}
catch (Exception)
{
return null;
}
}

我已将其精确定位到获取包含图像的文件夹的那一行。这甚至是获取嵌套文件夹的正确方法吗?

最佳答案

您可以尝试使用 ConfigureAwait(false) 在线程池线程上运行 for 循环:

public static async Task<StorageFolder> GetFileFolderAsync(String fileUrl)
{
try
{
string filePathRelative = DownloadedFilePaths.GetRelativeFilePathFromUrl(fileUrl);
string[] words = filePathRelative.Split('\\');
// HERE added ConfigureAwait call
StorageFolder currentFolder = await
DownloadedFilePaths.GetAppDownloadsFolder().ConfigureAwait(false);
// Code that follows ConfigureAwait(false) call will (usually) be
// scheduled on a background (non-UI) thread.
for (int i = 0; (i < words.Length - 1); i++)
{
// should no longer be on the UI thread,
// so scrollviewer will no longer block
currentFolder = await currentFolder.GetFolderAsync(words[i]);
}
return currentFolder;
}
catch (Exception)
{
return null;
}
}

请注意,在上述情况下,由于没有在 UI 上完成任何工作,您可以使用 ConfigureAwait(false)。例如,以下将不起作用,因为在 ConfigureAwait 之后有一个与 UI 相关的调用:

// HERE added ConfigureAwait call
StorageFolder currentFolder = await
DownloadedFilePaths.GetAppDownloadsFolder().ConfigureAwait(false);
// Can fail because execution is possibly not on UI thread anymore:
myTextBox.Text = currentFolder.Path;

关于c# - Windows 8 - 如何正确获取嵌套文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15979273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com