如何在C++/winrt中调用StorageFile.OpenReadAsync?
How to call StorageFile.OpenReadAsync in C++/winrt?
在我继续努力以 C++/winrt 加载一组 .svg 文件的过程中,我遇到了一个神秘的 link 错误。我想尝试使用 CanvasSvgDocument.ReadAsync(resourceCreator, filestream)。要到达那里,首先需要从 StorageFile 获取流,这似乎是实现它的方法(nextFile,一个 StorageFile,已经在此示例中加载)。这当然是在定义为 IAsyncAction 的方法中。我将从文件顶部列出#includes 和命名空间。
#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 "winrt/Windows.UI.Xaml.Media.h"
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::Storage::Provider;
using namespace Windows::Storage::Search;
using namespace Windows::Storage::Streams;
这是有问题的调用:
IRandomAccessStreamWithContentType fileStream = co_await nextFile.OpenReadAsync();
这会产生一个 link 错误,我将在下面放入该错误。我是否尝试 fileStream 结果的完全限定名称并不重要:
winrt::Windows::Storage::Streams::IRandomAccessStreamWithContentType fileStream = co_await nextFile.OpenReadAsync();
无论哪种方式我都会得到这个 link 错误:
Error LNK2019 unresolved external symbol "public: struct
winrt::Windows::Foundation::IAsyncOperation
__thiscall winrt::impl::consume_Windows_Storage_Streams_IRandomAccessStreamReference::OpenReadAsync(void)const "
(?OpenReadAsync@?$consume_Windows_Storage_Streams_IRandomAccessStreamReference@UIStorageFile@Storage@Windows@winrt@@@impl@winrt@@QBE?AU?$IAsyncOperation@UIRandomAccessStreamWithContentType@Streams@Storage@Windows@winrt@@@Foundation@Windows@3@XZ)
referenced in function "public: struct
winrt::Windows::Foundation::IAsyncAction __thiscall
AppEngine::ResourceManager::LoadSvgResources$_ResumeCoro(struct
winrt::Microsoft::Graphics::Canvas::UI::Xaml::CanvasControl)"
(?LoadSvgResources$_ResumeCoro@ResourceManager@AppEngine@@QAE?AUIAsyncAction@Foundation@Windows@winrt@@UCanvasControl@Xaml@UI@Canvas@Graphics@Microsoft@6@@Z)
我也尝试过使用 auto 作为结果类型,但没有成功。在 C++/winrt 中使用 OpenReadAsync() 获取流的正确方法是什么?
您显然是在启用 precompiled headers 的情况下进行编译(由 #include "pch.h"
指令提示)。这样做时,用于生成预编译的 header 的 header 必须 包含在第一个 non-empty, non-comment 行编译单元使用它。
/Yu (Use Precompiled Header File)文档有相关资料:
The compiler treats all code occurring before the .h file as precompiled. It skips to just beyond the #include directive associated with the .h file, uses the code contained in the .pch file, and then compiles all code after filename
.
换句话说,#include "pch.h"
指令之前的所有包含都将被忽略。由于 C++/WinRT 是一个 header-only 库,这最终会导致链接器错误。
解决问题
- 将
#include "pch.h"
指令移动到文件的最顶部,或者
- 将
#include
指令替换为相关编译单元上的/FI (Name Forced Include File)编译器选项,或
- 禁止使用预编译 headers。
在我继续努力以 C++/winrt 加载一组 .svg 文件的过程中,我遇到了一个神秘的 link 错误。我想尝试使用 CanvasSvgDocument.ReadAsync(resourceCreator, filestream)。要到达那里,首先需要从 StorageFile 获取流,这似乎是实现它的方法(nextFile,一个 StorageFile,已经在此示例中加载)。这当然是在定义为 IAsyncAction 的方法中。我将从文件顶部列出#includes 和命名空间。
#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 "winrt/Windows.UI.Xaml.Media.h"
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Storage;
using namespace Windows::Storage::Provider;
using namespace Windows::Storage::Search;
using namespace Windows::Storage::Streams;
这是有问题的调用:
IRandomAccessStreamWithContentType fileStream = co_await nextFile.OpenReadAsync();
这会产生一个 link 错误,我将在下面放入该错误。我是否尝试 fileStream 结果的完全限定名称并不重要:
winrt::Windows::Storage::Streams::IRandomAccessStreamWithContentType fileStream = co_await nextFile.OpenReadAsync();
无论哪种方式我都会得到这个 link 错误:
Error LNK2019 unresolved external symbol "public: struct winrt::Windows::Foundation::IAsyncOperation __thiscall winrt::impl::consume_Windows_Storage_Streams_IRandomAccessStreamReference::OpenReadAsync(void)const " (?OpenReadAsync@?$consume_Windows_Storage_Streams_IRandomAccessStreamReference@UIStorageFile@Storage@Windows@winrt@@@impl@winrt@@QBE?AU?$IAsyncOperation@UIRandomAccessStreamWithContentType@Streams@Storage@Windows@winrt@@@Foundation@Windows@3@XZ) referenced in function "public: struct winrt::Windows::Foundation::IAsyncAction __thiscall AppEngine::ResourceManager::LoadSvgResources$_ResumeCoro(struct winrt::Microsoft::Graphics::Canvas::UI::Xaml::CanvasControl)" (?LoadSvgResources$_ResumeCoro@ResourceManager@AppEngine@@QAE?AUIAsyncAction@Foundation@Windows@winrt@@UCanvasControl@Xaml@UI@Canvas@Graphics@Microsoft@6@@Z)
我也尝试过使用 auto 作为结果类型,但没有成功。在 C++/winrt 中使用 OpenReadAsync() 获取流的正确方法是什么?
您显然是在启用 precompiled headers 的情况下进行编译(由 #include "pch.h"
指令提示)。这样做时,用于生成预编译的 header 的 header 必须 包含在第一个 non-empty, non-comment 行编译单元使用它。
/Yu (Use Precompiled Header File)文档有相关资料:
The compiler treats all code occurring before the .h file as precompiled. It skips to just beyond the #include directive associated with the .h file, uses the code contained in the .pch file, and then compiles all code after
filename
.
换句话说,#include "pch.h"
指令之前的所有包含都将被忽略。由于 C++/WinRT 是一个 header-only 库,这最终会导致链接器错误。
解决问题
- 将
#include "pch.h"
指令移动到文件的最顶部,或者 - 将
#include
指令替换为相关编译单元上的/FI (Name Forced Include File)编译器选项,或 - 禁止使用预编译 headers。