2

ディスクから画像をロードするオブジェクトのコレクションにバインドされた GridView があります。

オブジェクトは可視になるとスタックに置かれ、画像はスタックから順次ロードされます。

問題は、オブジェクトを含む ScrollViewer がスクロールを停止するまで GetFolderAsync() が戻らないことです。

コードは次のとおりです。

    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;
        }
    }

画像を含むフォルダーを取得する行まで特定しました。これは、ネストされたフォルダーを取得する適切な方法ですか?

4

2 に答える 2

1

スレッドプールスレッドでループ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)。たとえば、 の後に UI 関連の呼び出しがあるため、次のコードは機能しませんConfigureAwait

// 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;
于 2013-04-12T20:23:43.213 に答える
0

オブジェクトの可視性を判断するために使用していた方法が、UI スレッドをブロックしていたことが判明しました。

ディスクから画像をロードするオブジェクトのコレクションにバインドされた GridView があります。

オブジェクトは表示されるとスタックに置かれ、画像はスタックから順次ロードされます。

問題は、オブジェクトを含む ScrollViewer がスクロールを停止するまで GetFolderAsync() が戻らないことです。

于 2013-04-12T22:14:03.193 に答える