C ++ CX Windows RT create_task使用return vs C#Confusion [英] C++ CX Windows RT create_task working with returns vs C# Confusion

查看:60
本文介绍了C ++ CX Windows RT create_task使用return vs C#Confusion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先道歉,如果这不是正确的论坛,其次,如果我完全忽略了这一点。


如何在不使用静态变量的情况下实现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;
//获取http数据,拆分并使用'tile'设置一些TextBlocks

C ++ CX 

 static String ^ markup = ref new String(); 

返回create_task(Package :: Current-> InstalledLocation-> GetFolderAsync(" Assets"))
.then([inputMarkupFilename](StorageFolder ^ assetsFolder) - > IAsyncOperation< StorageFile ^> ^
{
return assetsFolder-> GetFileAsync(inputMarkupFilename);
}
).then([](StorageFile ^ markupStorageFile) - > IAsyncOperation< Platform :: String ^> ^
{
返回FileIO :: ReadTextAsync(markupStorageFile);
}
//).then([this,outputImageFilename,size](平台: :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"));
}
).t hen([this,outputImageFilename,size](Platform :: String ^ httpContent)
{
Border ^ border =(Border ^)XamlReader :: Load(markup);

//获取http数据,拆分并使用'tile'设置一些TextBlocks
//返回...

});





解决方案

您需要通过引用捕获变量


http://en.cppreference.com/w/cpp/language/lambda



例如

 String ^ value = nullptr; 

create_task(MyAsyncMethod())。then([& value](T ^ asyncResult)
{
value =" this is the information" ;;
});


Firstly apologies if this isn't the right forum and secondly if I am totally missing the point.

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

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

});


解决方案

you need to capture a variable by reference

http://en.cppreference.com/w/cpp/language/lambda

And for example

String^ value=nullptr;

create_task(MyAsyncMethod()).then([&value](T^ asyncResult)
{
   value="this is the information";
});


这篇关于C ++ CX Windows RT create_task使用return vs C#Confusion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆