C++ C++/CX WinRT文件副本
我真的在忍受WinRT Windows::Storage命名空间的异步性 我的头文件中有以下私人成员:C++ C++/CX WinRT文件副本,c++,windows-runtime,c++-cx,C++,Windows Runtime,C++ Cx,我真的在忍受WinRT Windows::Storage命名空间的异步性 我的头文件中有以下私人成员: //Members for copying the SQLite db file Platform::String^ m_dbName; Windows::Storage::StorageFolder^ m_localFolder; Windows::Storage::StorageFolder^ m_installFolder; Windows::Storage::StorageFile^
//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);
});
}
});
//确保SQLite数据库的格式为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新建AsyncOperationCompletedHandler([this](IAsyncOperation^操作,异步状态)
{
m_dbFile=operation->GetResults();
if(m_dbFile==nullptr)
{
auto getInstalledFileOp=m_installFolder->GetFileAsync(m_dbName);
getInstalledFileOp->Completed=ref新建AsyncOperationCompletedHandler([此](IAsyncOperation^操作,异步状态)
{
m_dbFile=operation->GetResults();
m_dbFile->copyanc(m_localFolder,m_dbName);
});
}
});
当到达m_dbFile=operation->GetResults()时,我会遇到内存访问冲突代码>
我错过了什么?我来自一个c#的背景,在这个背景下做这件事真的很容易:/
我尝试使用“.then”而不是注册事件,但我甚至无法编译它们
谢谢你的帮助 如果您对WinRT解决方案感兴趣,这里是:
您似乎只想将DB文件从安装位置复制到本地文件夹中。为此,以下代码应足够:
//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;
create_task(m_installFolder->GetFileAsync(m_dbName)).then([this](StorageFile^ file)
{
create_task(file->CopyAsync(m_localFolder, m_dbName)).then([this](StorageFile^ copiedFile)
{
// do something with copiedFile
});
});
我以前试过这个东西。不要这样做:
if(m_dbFile == nullptr)
而是验证“状态”的值
我还没有机会查看你的代码,但是作为一个同步的选择,考虑CopyFiel2,它可以从Windows存储App.Yes调用,看起来更好:)这些链接解释了C++的异步编程模型:尝试在代码中替换<代码> > <代码> >在代码中捕获的变量>代码> >代码>。但是=
将只捕获那些在lambda相同范围内的变量。如果变量m_installFolder
、m_localFolder
和m_dbName
与lambda在同一范围内,则可以。如果没有,请在方括号中明确说明。
if(status == AsyncStatus::Error)