C ++ CX Windows RT create_task使用return vs C#Confusion

时间:2015-02-11 02:06:43

标签: c# task-parallel-library c++-cx windows-rt

如何在不使用静态变量的情况下实现C ++ CX中下面的C#代码示例这么简单的事情,当然这很可怕。

C#:

var folder = awaitWindows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
var file = await folder.GetFileAsync("customTile.xml");
string szCustomTileXML = await Windows.Storage.FileIO.ReadTextAsync(file);
HttpClient c = new HttpClient();
await var s = c->GetStringAsync(new Uri("www.bing.com"));
Border tile = XamlReader.Load(szCustomTileXML) as Border;
// Take http data, split it and using 'tile' set some TextBlocks

我能在C ++ Cx中看到这样做的唯一方法:

static String^ markup = ref new String();

    return create_task(Package::Current->InstalledLocation->GetFolderAsync("Assets"))
    .then([inputMarkupFilename](StorageFolder^ assetsFolder) ->IAsyncOperation<StorageFile^>^ 
    {
        return assetsFolder->GetFileAsync(inputMarkupFilename);
    }
    ).then([](StorageFile^ markupStorageFile)  ->IAsyncOperation<Platform::String^>^ 
    {
        return FileIO::ReadTextAsync(markupStorageFile);
    } //untouched upto here
    //).then([this, outputImageFilename, size](Platform::String^ markupContent)
    ).then([this, outputImageFilename, size](Platform::String^ markupContent) -> Windows::Foundation::IAsyncOperationWithProgress<Platform::String^, Windows::Web::Http::HttpProgress>^
    {
        markup = markupContent;

        HttpClient ^hc = ref new HttpClient();

        return hc->GetStringAsync(ref new Uri("www.bing.com"));
    }
    ).then([this, outputImageFilename, size](Platform::String^ httpContent) 
    {
        Border^ border = (Border^)XamlReader::Load(markup);

// Take http data, split it and using 'tile' set some TextBlocks
// return ...

});

1 个答案:

答案 0 :(得分:0)

这应该有效

    task<StorageFolder^>(Package::Current->InstalledLocation->GetFolderAsync("Assets")).then([](StorageFolder^ folder) {
    task<StorageFile^>(folder->GetFileAsync("customTile.xml")).then([](StorageFile^ file) {
        task<String^>(FileIO::ReadTextAsync(file)).then([](String^ markupContent) {
            auto hc = ref new HttpClient();
            task<String^>(hc->GetStringAsync(ref new Uri("www.bing.com"))).then([markupContent](String^ httpContent) {
                auto border = (Border^) XamlReader::Load(markupContent);
                // Take http data, split it and using 'tile' set some TextBlocks
            });
        });
    });
});