1

How do I achieve something as simple as the C# code sample below in C++ CX without using a static variable which of course if horrible.

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

The only way I could see to do this in 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 ...

});
Stefan V
  • 33
  • 4

1 Answers1

0

This should work

    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
            });
        });
    });
});
Raman Sharma
  • 4,551
  • 4
  • 34
  • 63