2015-02-11 59 views
1

如何在不使用靜態變量(當然,如果可怕)的情況下實現像C++ CX中C#代碼示例一樣簡單的操作。C++ CX Windows RT create_task使用返回值與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 ... 

}); 

回答

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