0

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

#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 (枚举 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(void)" (?LoadActivities$_ResumeCoro$2@ResourceManager@AppEngine@@QAE?AUIAsyncAction@Foundation@Windows@winrt@@XZ)

(后跟目标文件的路径)

我不得不承认我发现这个错误信息很难破译。也许这里的其他人会有一个想法?一定是在最近的系统更新中发生了变化。

4

1 回答 1

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();
}
于 2018-02-12T02:03:46.450 回答