首页 文章

如何在C / winrt中调用StorageFile.OpenReadAsync?

提问于
浏览
0

在我继续努力在C / winrt中加载.svg文件的集合时,我遇到了一个神秘的链接错误 . 我想尝试使用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();

这会产生一个链接错误,我将在下面提出 . 无论我是否尝试fileStream结果的完全限定名称都无关紧要:

winrt :: Windows :: Storage :: Streams :: IRandomAccessStreamWithContentType fileStream = co_await nextFile.OpenReadAsync();

无论哪种方式,我得到此链接错误:

错误LNK2019未解析的外部符号“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 @UrdsandomAccessStreamWithContentType @ Streams @ Storage @Windows @ winrt @@@ Foundation @Windows @ 3 @XZ)在函数“public:struct winrt :: Windows :: Foundation”中引用:: IAsyncAction __thiscall AppEngine :: ResourceManager :: LoadSvgResources $ _ResumeCoro $ 2(struct winrt :: Microsoft :: Graphics :: Canvas :: UI :: Xaml :: CanvasControl)“(?LoadSvgResources $ _ResumeCoro $ 2 @ ResourceManager @ AppEngine @@ QAE ?AUIAsyncAction @基金会@ @的Windows WinRT的@@ UCanvasControl @ @的XAML UI @帆布@图形@微软@ 6 @@ Z)

我也试过使用auto作为结果类型,没有运气 . 在C / winrt中使用OpenReadAsync()获取流的正确方法是什么?

1 回答

  • 1

    您显然正在使用precompiled headers进行编译(由 #include "pch.h" 指令暗示) . 执行此操作时,用于生成预编译头的标头 must 将包含在使用它的编译单元中的第一个非空的非注释行中 .

    /Yu (Use Precompiled Header File)文档包含相关信息:

    编译器将.h文件之前出现的所有代码视为预编译 . 它跳到与.h文件关联的#include指令之外,使用.pch文件中包含的代码,然后在文件名后编译所有代码 .

    换句话说,忽略 #include "pch.h" 指令之前的所有包含 . 由于C / WinRT是仅限标头的库,因此最终会导致链接器错误 .

    解决问题

    • #include "pch.h" 指令移动到文件的最顶部,或

    • #include 指令替换为相关编译单元上的/FI (Name Forced Include File)编译器选项,或

    • 禁用预编译头的使用 .

相关问题