locked
Trying to access resource files in WinRT

    Question

  • I'm currently working on a metro app that requires a few textual resources. Part of the build process is copying all of these resources to a folder inside of the app's installation directory. What I'd like to do is gather a list of these resource files, and process each one accordingly. Unfortunately, my attempts to do so have been less than successful.

    I've been trying to get the job done using the WinRT Asynchronous file IO operations.

    auto getResourceFolder = installedLocation->GetFolderFromPathAsync(  folderPath  );

    getResourceFolder
    ->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler< Windows::Storage::StorageFolder^ >(
    [this]( Windows::Foundation::IAsyncOperation< Windows::Storage::StorageFolder^ >^ operation ) {

       
    if( operation->Status == Windows::Foundation::AsyncStatus::Completed ) {

           
    auto resourceFolder = operation->GetResults();
           
    auto getResourceFiles = resourceFolder->GetFilesAsync();
            getResourceFiles
    ->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler< IVectorView< Windows::Storage::IStorageFile^ >^ >(
           
    [this]( Windows::Foundation::IAsyncOperation< IVectorView< Windows::Storage::IStorageFile^ >^ >^ operation ) {

               
    if( operation->Status == Windows::Foundation::AsyncStatus::Completed ) {

                   
    auto resourceFiles = operation->GetResults();

                   
    for( unsigned int i = 0; i < resourceFiles->Size; ++i ) {

                       
    // Process File
                   
    }

               
    }

           
    });

       
    }

    });

    Which fails to compile:

    error C2664: 'Windows::Foundation::IAsyncOperation<TResult>::Completed::set' : cannot convert parameter 1 from 'Windows::Foundation::AsyncOperationCompletedHandler<TResult> ^' to 'Windows::Foundation::AsyncOperationCompletedHandler<TResult> ^'

    The error isn't making any sense to me. I've tried rewriting the above code so that the lambda handler functions are not inline, but it hasn't made a difference. I'm not sure what's wrong.

    Any ideas? Thanks in advance.

    Saturday, March 31, 2012 12:26 AM

Answers

  • Hello,

    First, Windows::Foundation::AsyncOperationCompletedHandler< IVectorView< Windows::Storage::IStorageFile^ >^ >

    Should be Windows::Storage::StorageFile.

    Second, ref new Windows::Foundation::AsyncOperationCompletedHandler take two parameters.

    Please follow these codes

    	auto getResourceFolder = installedLocation->GetFolderFromPathAsync(  folderPath  );
    getResourceFolder->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler< Windows::Storage::StorageFolder^ >( [this]( Windows::Foundation::IAsyncOperation< Windows::Storage::StorageFolder^ >^ operation,AsyncStatus status ) { if( status == Windows::Foundation::AsyncStatus::Completed ) { auto resourceFolder = operation->GetResults(); IAsyncOperation<IVectorView< Windows::Storage::StorageFile^>^>^ getResourceFiles = resourceFolder->GetFilesAsync(); getResourceFiles->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler< IVectorView< Windows::Storage::StorageFile^ >^ >( [this]( Windows::Foundation::IAsyncOperation< IVectorView< Windows::Storage::StorageFile^ >^ >^ operation ,AsyncStatus status ) { if( status == Windows::Foundation::AsyncStatus::Completed ) { auto resourceFiles = operation->GetResults(); for( unsigned int i = 0; i < resourceFiles->Size; ++i ) { // Process File } } }); }});

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Jesse Jiang Monday, April 02, 2012 8:39 AM
    • Marked as answer by JSAndrew Monday, April 02, 2012 8:10 PM
    Monday, April 02, 2012 8:39 AM

All replies

  • Hello,

    First, Windows::Foundation::AsyncOperationCompletedHandler< IVectorView< Windows::Storage::IStorageFile^ >^ >

    Should be Windows::Storage::StorageFile.

    Second, ref new Windows::Foundation::AsyncOperationCompletedHandler take two parameters.

    Please follow these codes

    	auto getResourceFolder = installedLocation->GetFolderFromPathAsync(  folderPath  );
    getResourceFolder->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler< Windows::Storage::StorageFolder^ >( [this]( Windows::Foundation::IAsyncOperation< Windows::Storage::StorageFolder^ >^ operation,AsyncStatus status ) { if( status == Windows::Foundation::AsyncStatus::Completed ) { auto resourceFolder = operation->GetResults(); IAsyncOperation<IVectorView< Windows::Storage::StorageFile^>^>^ getResourceFiles = resourceFolder->GetFilesAsync(); getResourceFiles->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler< IVectorView< Windows::Storage::StorageFile^ >^ >( [this]( Windows::Foundation::IAsyncOperation< IVectorView< Windows::Storage::StorageFile^ >^ >^ operation ,AsyncStatus status ) { if( status == Windows::Foundation::AsyncStatus::Completed ) { auto resourceFiles = operation->GetResults(); for( unsigned int i = 0; i < resourceFiles->Size; ++i ) { // Process File } } }); }});

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Jesse Jiang Monday, April 02, 2012 8:39 AM
    • Marked as answer by JSAndrew Monday, April 02, 2012 8:10 PM
    Monday, April 02, 2012 8:39 AM
  • Thanks Jesse!
    Monday, April 02, 2012 8:10 PM
  • As an addendum, you can do this a bit more compactly with ppl tasks:

    #include <ppltasks.h>

    using namespace concurrency;

    task<StorageFolder^>(installedLocation->GetFolderFromPathAsync(folderPath))
    	.then([&](StorageFolder^ resourceFolder)
    {
    	task<IVectorView<Windows::Storage::StorageFile^>^>(resourceFolder->GetFilesAsync())
    				.then([&](IVectorView< Windows::Storage::StorageFile^>^ resourceFiles)
    			{
    				for(auto file : resourceFiles)
    				{
    					// Process File
    					::OutputDebugStringW(String::Concat(file->Name, L"\n")->Data());
    				}
    			});
    });
    
    -Steve
    Tuesday, April 03, 2012 12:33 AM
    Moderator