积极答复者
如何像C++一样在应用商店应用中读取文件?

问题
-
我的代码是:
auto openPicker = ref new Windows::Storage::Pickers::FileOpenPicker(); openPicker->FileTypeFilter->Append("*"); create_task(openPicker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file) { if (file != nullptr){ auto userContent = Windows::Storage::FileIO::ReadTextAsync(file); text->Text = userContent->ToString(); } });
我希望输出的是文件中的字符,但是实际输出是
Windows.Foundation.IAsyncOperation`1<String>
应该怎样更改?
答案
-
ReadTextAsync和PickSingleFileAsync一样,都是异步操作,所以你需要跟PickSingleFileAsync一样使用create_task,then来在操作完成后获取数据
- 已标记为答案 lxylxy123456 2014年4月29日 12:33
-
使用create_task以后还是没有输出正确的文字,代码:
auto openPicker = ref new Windows::Storage::Pickers::FileOpenPicker(); openPicker->FileTypeFilter->Append("*"); create_task(openPicker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file) { if (file != nullptr){ create_task(Windows::Storage::FileIO::ReadTextAsync(file)).then([this](String^ text_){ text->Text = text_; }); } });
- 已标记为答案 lxylxy123456 2014年4月29日 12:33
-
没有输出正确文字的原因是Windows::Storage::FileIO::ReadTextAsync(file)返回的不是文字,而是一个异步任务对象IAsyncOperation<String^>,需要执行这个异步任务才能获取文字。
我测试了一下你修改后的代码,发现在我的电脑上是能正确的读取文本的。所以你可能要设断点调试一下才能知道为什么在你的电脑上没有能够正确输出文字。有可能file不是正确的文本文件或者其他原因,得调试才能知道了。
- 已标记为答案 lxylxy123456 2014年4月29日 12:33
全部回复
-
ReadTextAsync和PickSingleFileAsync一样,都是异步操作,所以你需要跟PickSingleFileAsync一样使用create_task,then来在操作完成后获取数据
- 已标记为答案 lxylxy123456 2014年4月29日 12:33
-
使用create_task以后还是没有输出正确的文字,代码:
auto openPicker = ref new Windows::Storage::Pickers::FileOpenPicker(); openPicker->FileTypeFilter->Append("*"); create_task(openPicker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file) { if (file != nullptr){ create_task(Windows::Storage::FileIO::ReadTextAsync(file)).then([this](String^ text_){ text->Text = text_; }); } });
- 已标记为答案 lxylxy123456 2014年4月29日 12:33
-
没有输出正确文字的原因是Windows::Storage::FileIO::ReadTextAsync(file)返回的不是文字,而是一个异步任务对象IAsyncOperation<String^>,需要执行这个异步任务才能获取文字。
我测试了一下你修改后的代码,发现在我的电脑上是能正确的读取文本的。所以你可能要设断点调试一下才能知道为什么在你的电脑上没有能够正确输出文字。有可能file不是正确的文本文件或者其他原因,得调试才能知道了。
- 已标记为答案 lxylxy123456 2014年4月29日 12:33