Question

I have several images being animated inside a Grid container and I need to remove them from the Grid after the animation ends to save memory.

storyboard->Completed += ref new EventHandler<Object^> 
   ([this,birthImage,&index](Object^ sender, Object^ e) {
        mainGrid->Children->IndexOf (myImage,  &index);
        mainGrid->Children->RemoveAt  (index);
    });

Unfortunately I cannot follow this suggestion and use mainGrid->Chilren->Remove(myImage) because this method is available only for C# and not for C++/CX

Being forced to use IndexOf and then RemoveAt causes concurrency problems.

I need a solution to remove an object from a View Hierarchy in C++/CX

Something that in the iOS world could be done with the single: [object removeFromSuperView];

Was it helpful?

Solution

In the class header I've declared

private:
   concurrency::reader_writer_lock myLock;

And changed the implementation to:

storyboard->Completed += ref new EventHandler<Object^> 
   ([this,birthImage](Object^ sender, Object^ e) {
        unsigned int index;
        myLock.lock();
        if (mainGrid->Children->IndexOf (myImage,  &index))
        {
              mainGrid->Children->RemoveAt (index);
        }
        myLock.unlock();
    });

Notice the declaration of unsigned int index as a local variable to the lambda function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top