Programmatically removing an object from a grid in Windows Metro

I have several images animated inside the Grid container, and I need to remove them from the Grid after the animation is finished 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 can be done with a single: [object removeFromSuperView];

+4
source share
1 answer

In the class header, I 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(); }); 

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

+1
source

Source: https://habr.com/ru/post/1499831/


All Articles