2012-07-12 58 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