如何在2018年10月枚举文件夹内容

时间:2018-02-11 21:59:41

标签: c++-winrt

尝试将StorageFolder方法转换为cppwinrt GetFilesAsync我无法通过编译器链接错误。这是一个非常简单的例程来测试这个概念:

#include "winrt/Windows.Storage.h" 
#include "winrt/Windows.Foundation.Collections.h"
IAsyncAction TestClass::LoadFiles()
{
    StorageFolder appFolder = Windows::ApplicationModel::Package::Current().InstalledLocation(); 
    StorageFolder assetsFolder = co_await appFolder.GetFolderAsync(hstring(L"Assets")); 
    auto files = co_await assetsFolder.GetFilesAsync(CommonFileQuery::DefaultQuery); 
}

问题似乎在于GetFilesAsync的返回类型。我尝试了各种类型,例如IVectorView,但似乎没有任何工作。有没有人知道一个代码示例,显示如何在C ++ / winrt中完成此枚举?

[更新]使用SDK 10.0.17666和VS 15.9.0 Preview 3返回此项目,我发现之前从这些答案中采用的解决方案不再有效。这一次,我一定会包含完整的错误,看看是否有人有想法。为简单起见,我将仅使用IInspectable提供的简单代码,仅将其更改为使其成为ResourceManager类中的类成员:

 #include "winrt/Windows.ApplicationModel.h"
#include "winrt/Windows.Storage.h"
#include "winrt/Windows.Storage.Streams.h"
#include "winrt/Windows.Foundation.Collections.h"
#include "winrt/Windows.Storage.Search.h"
#include "winrt/Windows.UI.Core.h"
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Search;

IAsyncAction ResourceManager::LoadActivities()
{
    StorageFolder appFolder = Windows::ApplicationModel::Package::Current().InstalledLocation();
    StorageFolder assetsFolder = co_await appFolder.GetFolderAsync(L"Activities");
    auto files = co_await assetsFolder.GetFilesAsync(CommonFileQuery::DefaultQuery);
}

对GetFilesAsync的调用现在会产生以下链接错误: 严重性代码描述项目文件行抑制状态 错误LNK2019未解析的外部符号“public:struct winrt :: Windows :: Foundation :: IAsyncOperation> __thiscall winrt :: impl :: consume_Windows_Storage_Search_IStorageFolderQueryOperations :: GetFilesAsync(enum winrt :: Windows :: Storage :: Search :: CommonFileQuery const& )const“(?GetFilesAsync @?$ consume_Windows_Storage_Search_IStorageFolderQueryOperations @ UStorageFolder @ Storage @Windows @ winrt @@@ impl @ winrt @@ QBE?AU?$ IAsyncOperation @ U?$ IVectorView @ UStorageFile @ Storage @Windows @ winrt @@@ Collections @ Foundation @Windows @ winrt @@@ Foundation @ Windows @ 3 @ ABW4CommonFileQuery @ Search @ Storage @ 63 @@ Z)在函数“public:struct winrt :: Windows :: Foundation :: IAsyncAction __thiscall AppEngine :: ResourceManager :: LoadActivities”中引用$ _ResumeCoro $ 2(无效)“(?LoadActivities $ _ResumeCoro $ 2 @ ResourceManager @ AppEngine @@ QAE?AUIAsyncAction @Foundation @Windows @ winrt @@ XZ)

(后跟目标文件的路径)

我必须承认我发现该错误信息难以破译。也许这里的其他人会有想法?必须是最近系统更新中发生变化的内容。

1 个答案:

答案 0 :(得分:1)

对于它的价值,以下独立代码构建得很好。所以你可能要么错过了一个#include或一个链接库,但是当你没有分享重要信息时就不可能知道,例如你看到的实际错误

#pragma comment(lib, "WindowsApp")

#include <winrt/Windows.ApplicationModel.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Search.h>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Search;

IAsyncAction LoadFiles()
{
    StorageFolder appFolder = Windows::ApplicationModel::Package::Current().InstalledLocation();
    StorageFolder assetsFolder = co_await appFolder.GetFolderAsync(L"Assets");
    auto files = co_await assetsFolder.GetFilesAsync(CommonFileQuery::DefaultQuery);
}

int main()
{
    LoadFiles().get();
}