【问题标题】:C++ CX Windows RT create_task working with returns vs C# ConfusionC++ CX Windows RT create_task 使用返回与 C# 混淆
【发布时间】:2015-05-26 10:01:06
【问题描述】:

如何在不使用静态变量的情况下,在 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 ...

});

【问题讨论】:

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


    【解决方案1】:

    这应该可以工作

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 2012-05-20
      • 2014-11-18
      • 2021-04-07
      • 1970-01-01
      相关资源
      最近更新 更多