locked
Share charm is picky about its data

    Question

  • I am trying to construct an array of files for the share charm.  I copied most of my code from the share example.  If I use the following code to generate "storageItems" the share charm works properly:

    void Session::initStorageItems()
    {
        SmartRecorderSession^ session = ((SmartRecorderSession^)SRSession->DataContext);
        storageItems = nullptr;
        create_task(session->folder->GetFilesAsync()).then([=](IVectorView<StorageFile^>^ files)
        {
            int x = 0;
            StorageFile^ file = nullptr;
            storageItems = files;
        });
    }

    However, if I use the following slightly more complicated code, then the share charm crashes:

    void Session::initStorageItems()
    {
        SmartRecorderSession^ session = ((SmartRecorderSession^)SRSession->DataContext);
        storageItems = nullptr;
        create_task(session->folder->GetFilesAsync()).then([=](IVectorView<StorageFile^>^ files)
        {
            int x = 0;
            StorageFile^ file = nullptr;
            for (x = 0; x < files->Size; ++x) {
                String^ n = files->GetAt(x)->Name;
                if (!n->Equals("Recording.wav")) {
                    file = files->GetAt(x);
                }
            }
            if (file != nullptr) {
                Vector<StorageFile^>^ filearr=ref new Vector<StorageFile^>();
                shareFile = file;
                //fileArray = filearr;
                filearr->Append(file);
                //Platform::Collections::VectorView<StorageFile^>^ files = ref new Platform::Collections::VectorView<StorageFile^>(filearr);
                //Windows::Foundation::Collections::IVectorView<StorageFile^>^ xx = files;
                storageItems = filearr->GetView();
            }
        });
    }

    Here is the GetShareContent function which uses storageItems:

    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;
    }

    It is copied from the share charm example (along with everything else that was needed).

    What do I need to do to work around this issue?

                       - Ken


    - Ken

    Wednesday, September 26, 2012 12:05 PM