0

create_task の実装外で Uri^ を定義しようとしています。Java では、非同期タスクに final 修飾子を追加すると、非同期コード内でその変数 (final 修飾子付き) を使用できるようになります。

非同期コード内で以下のコードから Uri^ ソースを使用するにはどうすればよいですか?

void addDownload(Uri^ source, StorageFolder^ destinationFolder, String^ fileName) {
    boolean requestUnconstrainedDownload = false;
    IAsyncOperation<StorageFile^>^ asyncOperationStorageFile = destinationFolder->CreateFileAsync(fileName, CreationCollisionOption::GenerateUniqueName);
    auto createStorageFileTask = create_task(asyncOperationStorageFile);
    createStorageFileTask.then([] (StorageFile^ destinationFile) {
        BackgroundDownloader^ downloader = ref new BackgroundDownloader();
        DownloadOperation^ downloadOperation = downloader->CreateDownload(source, destinationFile);
        downloadOperation->Priority = BackgroundTransferPriority::Default;
        HandleDownloadAsync(downloadOperation, true);
    });
}
4

1 に答える 1

2

タスクのラムダ本体でアクセスできるように、変数ソースをラムダでキャプチャするだけです。

void addDownload(Uri^ source, StorageFolder^ destinationFolder, String^ fileName) {

boolean requestUnconstrainedDownload = false;
IAsyncOperation<StorageFile^>^ asyncOperationStorageFile = destinationFolder->CreateFileAsync(fileName, CreationCollisionOption::GenerateUniqueName);
auto createStorageFileTask = create_task(asyncOperationStorageFile);
createStorageFileTask.then([source] (StorageFile^ destinationFile) {
    BackgroundDownloader^ downloader = ref new BackgroundDownloader();
    DownloadOperation^ downloadOperation = downloader->CreateDownload(source, destinationFile);
    downloadOperation->Priority = BackgroundTransferPriority::Default;
    HandleDownloadAsync(downloadOperation, true);
});

}
于 2014-07-13T14:43:54.157 に答える