locked
Creating an IVectorView list of storageFolders

    Question

  • I have constructed a variable of type Vector as follows:

    Vector<StorageFile^>^ files;

    files->Append(file1);

    files->Append(file2);

    ..

    Now I want to create an IVectorView<StorageFile^>^ object based on this vector.  What is the easiest way to do this?

                     - Ken


    - Ken

    Sunday, September 23, 2012 3:40 AM

Answers

  • Hi,

    The VectorView constructor does not have Vector as parameters. We need to use array to convert them.

    	Platform::Collections::Vector<StorageFile^>^ files=ref new Platform::Collections::Vector<StorageFile^>();
    	files->Append(file1);
    	files->Append(file2);
    
    	Array<StorageFile^>^ filearr=ref new Array<StorageFile^>(files->Size);
    	for(int i=0;i<files->Size;i++)
    	{
    		filearr->set(i,files->GetAt(i));
    	}
    	Platform::Collections::VectorView<StorageFile^>^ vectviews =ref new Platform::Collections::VectorView<StorageFile^>(filearr);


    Best regards,
    Jesse


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

    • Proposed as answer by Jesse Jiang Friday, September 28, 2012 8:22 AM
    • Marked as answer by Jesse Jiang Monday, October 01, 2012 7:25 AM
    Monday, September 24, 2012 6:19 AM
  • IVectorView provides read only access to an IVector. It is created from the vector using IVector::GetView().

    Thus...

        StorageFile ^file1, ^file2;
    
        auto files = ref new Platform::Collections::Vector<StorageFile^>;
    
        files->Append(file1);
        files->Append(file2);
        auto filesView = files->GetView(); // return the IVectorView for this vector

     
    • Edited by Chris Guzak Wednesday, September 26, 2012 5:32 AM added comment on last line
    • Proposed as answer by Jesse Jiang Friday, September 28, 2012 8:21 AM
    • Marked as answer by Jesse Jiang Monday, October 01, 2012 7:25 AM
    Wednesday, September 26, 2012 5:31 AM

All replies

  • Hi,

    The VectorView constructor does not have Vector as parameters. We need to use array to convert them.

    	Platform::Collections::Vector<StorageFile^>^ files=ref new Platform::Collections::Vector<StorageFile^>();
    	files->Append(file1);
    	files->Append(file2);
    
    	Array<StorageFile^>^ filearr=ref new Array<StorageFile^>(files->Size);
    	for(int i=0;i<files->Size;i++)
    	{
    		filearr->set(i,files->GetAt(i));
    	}
    	Platform::Collections::VectorView<StorageFile^>^ vectviews =ref new Platform::Collections::VectorView<StorageFile^>(filearr);


    Best regards,
    Jesse


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

    • Proposed as answer by Jesse Jiang Friday, September 28, 2012 8:22 AM
    • Marked as answer by Jesse Jiang Monday, October 01, 2012 7:25 AM
    Monday, September 24, 2012 6:19 AM
  • This seems to work.  However, I am using the VectorView to implement the Share Charm functionality.  When I use the IVectorView object returned by GetFilesAsync, things work fine.  However, if I construct one as above, the Share Charm hangs or crashes.  The IVectorView constructed above is used in the following code: (saved as "storageFiles").

    bool Session::GetShareContent(Windows::ApplicationModel::DataTransfer::DataRequest^ request)
    {
        bool succeeded = false;
        SmartRecorderSession^ session = ((SmartRecorderSession^)SRSession->DataContext);

        if (storageItems != nullptr)
        {
            auto requestData = request->Data;
            requestData->Properties->Title = session->Title;

            // The description is optional.
            String^ dataPackageDescription = "Audio data from this session";
            if (dataPackageDescription != nullptr)
            {
                requestData->Properties->Description = dataPackageDescription;
            }
            requestData->SetStorageItems(dynamic_cast<IIterable<IStorageItem^>^>(storageItems));
            succeeded = true;
        }
        else
        {
            request->FailWithDisplayText("Select the files you would like to share and try again.");
        }
        return succeeded;
    }

    Note that I copied all of the Share Charm code from the "Sharing content source app"


    - Ken

    Monday, September 24, 2012 11:17 AM
  • IVectorView provides read only access to an IVector. It is created from the vector using IVector::GetView().

    Thus...

        StorageFile ^file1, ^file2;
    
        auto files = ref new Platform::Collections::Vector<StorageFile^>;
    
        files->Append(file1);
        files->Append(file2);
        auto filesView = files->GetView(); // return the IVectorView for this vector

     
    • Edited by Chris Guzak Wednesday, September 26, 2012 5:32 AM added comment on last line
    • Proposed as answer by Jesse Jiang Friday, September 28, 2012 8:21 AM
    • Marked as answer by Jesse Jiang Monday, October 01, 2012 7:25 AM
    Wednesday, September 26, 2012 5:31 AM