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

时间:2012-07-12 10:37:22

标签: c++ windows-8 windows-runtime windows-store-apps c++-cx

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

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

其中imageFile在头文件中定义

Windows::Storage::StorageFile^ imageFile;

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

An invalid parameter was passed to a function that considers invalid parameters fatal.

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

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

1 个答案:

答案 0 :(得分:5)

如果"纹理"来自您的应用程序包,您应该使用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;
});

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

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

如果您想使用GetFileFromPathAsync,我建议您使用

Windows::ApplicationModel::Package::Current->InstalledLocation

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