我真的很痛苦通過WinRT的視窗:存儲命名空間與所有它的asyncronousness。C++/CX WinRT的文件複製
我在我的頭文件中的下列私有成員:
//Members for copying the SQLite db file
Platform::String^ m_dbName;
Windows::Storage::StorageFolder^ m_localFolder;
Windows::Storage::StorageFolder^ m_installFolder;
Windows::Storage::StorageFile^ m_dbFile;
而且我在實現文件下面的代碼塊:
//Make sure the SQLite Database is in ms-appdata:///local/
m_dbName = L"DynamicSimulations.db";
m_localFolder = ApplicationData::Current->LocalFolder;
m_installFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
auto getLocalFileOp = m_localFolder->GetFileAsync(m_dbName);
getLocalFileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([this](IAsyncOperation<StorageFile^>^ operation, AsyncStatus status)
{
m_dbFile = operation->GetResults();
if(m_dbFile == nullptr)
{
auto getInstalledFileOp = m_installFolder->GetFileAsync(m_dbName);
getInstalledFileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([this](IAsyncOperation<StorageFile^>^ operation, AsyncStatus status)
{
m_dbFile = operation->GetResults();
m_dbFile->CopyAsync(m_localFolder, m_dbName);
});
}
});
我得到一個內存訪問衝突時,它得到到m_dbFile = operation->GetResults();
缺少什麼我在這裏?我來自一個C#背景中,這是很容易的東西做:/
我已經嘗試使用「於是」,而不是註冊的事件,但我甚至不能讓這些編譯。
謝謝你的幫助!
我還沒有機會查看您的代碼,但作爲同步替代方案,可以考慮CopyFile2,它可以從Windows應用商店應用中調用。 –
是的,這看起來好多了:) – Eric
此鏈接解釋異步編程模型與C++:http://msdn.microsoft.com/en-us/library/windows/apps/hh780559.aspx –