我编写
Windows Store App,我有以下问题.
我使用此代码将数字数组记录到文件中:
auto item = KnownFolders::PicturesLibrary;
task<StorageFile^> getFileTask(item->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting));
getFileTask.then([=](StorageFile^ storageFile)
{
return storageFile->OpenAsync(FileAccessMode::ReadWrite);
}).then([](IRandomAccessStream^ m_istream)mutable
{
unsigned char a[] = { 1, 2, 3 };
auto arr = ref new Array<unsigned char>(a, sizeof(a));
auto outs = m_istream->GetOutputStreamAt(0);
auto dw = ref new DataWriter(outs);
dw->WriteBytes(arr);
return dw->StoreAsync();
}).wait();
代码已成功编译,但它提供了一个错误:
MyTest.exe已触发断点.
它指向_REPORT_PPLTASK_UNOBSERVED_EXCEPTION(); line,可以在ppltasks.h中找到.
如果我使用.then([](DataWriterStoreOperation ^){})而不是.wait(),我的应用程序不会使用此错误进行编译:
error C2338: incorrect parameter type for the callable object in 'then';
consider _ExpectedParameterType or task<_ExpectedParameterType> (see below).
这是为什么?我用的是VS2013.请帮我.
最佳答案 我找到了这个问题的解决方案!
正确代码:
unsigned char a[] = { 1, 2, 3 };
auto arr = ref new Array<unsigned char>(a, sizeof(a));
auto m_istream = ref new InMemoryRandomAccessStream();
auto outs = m_istream->GetOutputStreamAt(0);
auto dw = ref new DataWriter(outs);
dw->WriteBytes(arr);
task<unsigned int>(dw->StoreAsync()).then([=](unsigned int)
{
return item->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting);
}).then([=](StorageFile^ storageFile)
{
return storageFile->OpenAsync(FileAccessMode::ReadWrite);
}).then([=](IRandomAccessStream^ new_stream)
{
return RandomAccessStream::CopyAsync(m_istream->GetInputStreamAt(0), new_stream->GetOutputStreamAt(0));
}).then([=](UINT64 copiedBytes)
{
return;
});