2015-04-01 50 views
0

我試圖使用WinRT API WIndows::System::Launcher::LaunchFileAsync()啓動圖像。WinRT API WIndows :: System :: Launcher :: LaunchFileAsync()用法從C++

代碼片段如下:

RoInitialize(RO_INIT_MULTITHREADED);     
String^ imagePath = ref new String(L"C:\\Users\\GoodMan\\Pictures\\wood.png"); 
auto file = Storage::StorageFile::GetFileFromPathAsync(imagePath); 
Windows::System::Launcher::LaunchFileAsync(file); 

我從LaunchFileAsync() API收到此錯誤:

錯誤C2665: '視窗::系統::啓動:: LaunchFileAsync': 2重載可以轉換所有參數類型

我可以請幫助如何解決這個問題。我對WinRT C++編碼非常陌生。

回答

2

方法GetFileFromPathAsync不返回StorageFile,但它返回IAsyncOperation<StorageFile>^。你所要做的是後者轉化爲前者,如下所示:

using namespace concurrency; 

String^ imagePath = ref new String(L"C:\\Users\\GoodMan\\Pictures\\wood.png"); 
auto task = create_task(Windows::Storage::StorageFile::GetFileFromPathAsync(imagePath)); 
task.then([this](Windows::Storage::StorageFile^ file) 
{ 
    Windows::System::Launcher::LaunchFileAsync(file); 
}); 

一般來說,在Async結束所有的Windows商店應用框架方法將返回一個IAsyncOperation或​​。這些方法就是所謂的異步方法,需要一些特殊的處理。有關更多信息,請參閱此文章:Asynchronous programming in C++


那麼現在一切都很好,正確嗎?好吧,不是。你的代碼還有另一個問題。這是當你運行上面的代碼時,你會得到一個訪問被拒絕的錯誤。原因是Windows商店應用程序是沙盒,並且通常不能訪問文件系統上的任何文件。

儘管如此,您還是很幸運,因爲您試圖訪問「圖片」文件夾中的文件。圖片文件夾是Windows應用商店應用有權訪問的特殊文件夾。您可以使用KnownFolders class得到它:

using namespace concurrency; 

Windows::Storage::StorageFolder^ pictures = 
    Windows::Storage::KnownFolders::PicturesLibrary; 
auto task = create_task(pictures->GetFileAsync("wood.png")); 

task.then([this](Windows::Storage::StorageFile^ file) 
{ 
    Windows::System::Launcher::LaunchFileAsync(file); 
}); 

注意,爲了訪問Pictures文件夾你的應用程序中聲明它的項目清單。爲此,請雙擊Visual Studio項目「樹」中的Package.appmanifest文件,然後選擇功能選項卡。然後根據功能,檢查圖片庫

相關問題