0
我想從我的資產文件夾打開一個XML文件,但不幸的是,我只能通過使用FileOpenPicker打開我的XML文件,這不是最理想的情況,當我必須不斷獲取我的XML文件,當然不會打擾用戶。C++/CX - GetFileAsync拋出斷點錯誤
FileOpenPicker^ openPicker = ref new FileOpenPicker();
openPicker->ViewMode = PickerViewMode::List;
openPicker->SuggestedStartLocation = PickerLocationId::Desktop;
openPicker->FileTypeFilter->Append(".xml");
task<StorageFile^>(
openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) {
if (nullptr != file) {
task<Streams::IRandomAccessStream^>(file->OpenAsync(FileAccessMode::Read)).then([this](Streams::IRandomAccessStream^ stream)
{
IInputStream^ deInputStream = stream->GetInputStreamAt(0);
DataReader^ reader = ref new DataReader(deInputStream);
reader->LoadAsync(stream->Size);
String^ strXml = reader->ReadString(stream->Size);
});
}
});
我現在正在嘗試將此代碼重新構建到加載我的xml文件而不讓用戶選擇的代碼中。我嘗試以下方法:
String^ xmlFile = "Assets\MyXmlFile.xml";
StorageFolder^ InstallationFolder = Windows::ApplicationModel::Package::Current->InstalledLocation;
task<StorageFile^>(
InstallationFolder->GetFileAsync(xmlFile)).then([this](StorageFile^ file) {
if (nullptr != file) {
task<Streams::IRandomAccessStream^>(file->OpenAsync(FileAccessMode::Read)).then([this](Streams::IRandomAccessStream^ stream)
{
IInputStream^ deInputStream = stream->GetInputStreamAt(0);
DataReader^ reader = ref new DataReader(deInputStream);
reader->LoadAsync(stream->Size);
String^ strXml = reader->ReadString(stream->Size);
stream->FlushAsync();
});
}
});
我覺得我得到的GetFileAsync對此我沒有能夠解決的錯誤,我問你,社區,試圖幫助我。
謝謝你的工作。它加載數據,但是當我多次啓動該方法時,出現錯誤。它表示如下:**操作嘗試訪問超出有效範圍的數據**在位置觸發:'String^strXml = reader-> ReadString(stream-> Size);' – Napokue