2012-07-12 61 views
3

我试图在Windows 8 Metro C++应用程序中使用共享魅力共享图像。为此,我需要首先将图像加载到StorageFile ^。我认为它应该是这样的:将图像从内容加载到StorageFile ^在Metro C++中

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

其中imageFile在头文件中定义

Windows::Storage::StorageFile^ imageFile; 

这实际代码将抛出此exeption

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

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

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

回答

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)不是绝对路径,并且不允许使用相对路径。

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

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

搞清楚的是安装了应用程序在那里,并从那里建立你的路径。

+0

谢谢!解决方案看起来非常微不足道! – 2012-07-24 23:46:19