在Metro C++中将图像从内容加载到StorageFile ^

Ale*_*jak 3 c++ windows-8 windows-runtime c++-cx windows-store-apps

我正在尝试使用Share Charm在Windows 8 Metro C++应用程序中共享图像.为此,我需要先将图像加载到StorageFile ^.我认为它应该看起来像:

create_task(imageFile->GetFileFromPathAsync("Textures/title.png")).then([this](StorageFile^ storageFile)
    {
        imageFile = storageFile;
    });
Run Code Online (Sandbox Code Playgroud)

其中imageFile在头文件中定义

Windows::Storage::StorageFile^ imageFile;
Run Code Online (Sandbox Code Playgroud)

这个实际的代码会抛出这个例子

An invalid parameter was passed to a function that considers invalid parameters fatal.
Run Code Online (Sandbox Code Playgroud)

这似乎非常简单,但是关于在Metro中共享的文档很少,并且唯一的Microsoft示例显示了如何使用FilePicker进行共享.

如果有人知道如何正确地做到这一点,将非常感激.

And*_*ich 5

如果"Textures"来自您的应用程序包,则应使用StorageFile :: GetFileFromApplicationUriAsync:

Uri^ uri = ref new Uri("ms-appx:///Assets/Logo.png");

create_task(StorageFile::GetFileFromApplicationUriAsync(uri)).then([](task<StorageFile^> t)
{
    auto storageFile = t.get();
    auto f = storageFile->FileType;
});
Run Code Online (Sandbox Code Playgroud)

您还可以使用基于任务的延续(如上所示),以便更密切地检查异常信息.在您的情况下,内部异常是:指定的路径(Assets/Logo.png)包含一个或多个无效字符.

这是由于正斜杠,如果您将其更改为反斜杠,您将看到:指定的路径(Assets\Logo.png)不是绝对路径,并且不允许相对路径.

如果你想使用GetFileFromPathAsync我建议使用

Windows::ApplicationModel::Package::Current->InstalledLocation
Run Code Online (Sandbox Code Playgroud)

确定应用程序的安装位置,并从那里构建路径.