Forum Libraries, Components, and Plugins

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Properly removing UI and Scene

Hello guys,

I made a simple menu system, when clicked on a button to open another screen, I first try to clear current UI.Root and then open a new screen. However, sometimes it crashes throwing the "Collection was modified; enumeration operation may not execute." error and sometimes it works but then some UIElements lose their events, don't register clicks anymore.
So I guess I shouldn't be clearing UI.Root in a button click event, should delay it one frame maybe?
Right now on button Released event I'm calling:

(window is a child of UI.Root)
window.UnsubscribeFromAllEvents();
window.RemoveAllChildren();
window.Remove();

then in another class, adding new screen

window = new Window();
Root.AddChild(root);

Also something wrong happens with Scene when trying to restart a game, same way, on a button click. It loads fine, but after couple times restarting, scene stops calling some events randomly. For example these:

Scene.GetComponent().PhysicsPreStep += FixedUpdate;
Scene.ScenePostUpdate += PostUpdate;

I'm removing scene like this:

Scene.RemoveAllChildren();
Scene.RemoveAllComponents();
Scene.UnsubscribeFromAllEvents();
Scene.RemoveAllActions();
Scene.Remove();

Then restarting by

Scene = new Scene();
Scene.LoadXml();

in a completely new class instance.

So what would be the correct way to do things?

Best Answer

Answers

  • EgorBoEgorBo BYXamarin Team ✭✭✭
    Scene.RemoveAllChildren();
    Scene.RemoveAllComponents();
    Scene.UnsubscribeFromAllEvents();
    Scene.RemoveAllActions();
    Scene.Remove();
    

    Scene.Remove() will do all the listed actions, you don't have to call them.

  • RayKoopaRayKoopa DEMember ✭✭

    @EgorBo said:
    Scene.RemoveAllChildren();
    Scene.RemoveAllComponents();
    Scene.UnsubscribeFromAllEvents();
    Scene.RemoveAllActions();
    Scene.Remove();

    Scene.Remove() will do all the listed actions, you don't have to call them.

    I necrobump this because I cannot agree with that from what I experienced triggers for a reply =P

    When I call Remove() on any scene instance, it does nothing. The documentation says it removes this node (since a scene is a node) from the parent, but the Parent of a scene is null. So nothing happens on my side. Only "RemoveAllChildren()" basically deletes anything of the scene.

  • elv1s42elv1s42 USMember

    Hi VinBa!
    Can you please say how you fixed it.

    It looks like I am facing the same issue.

Sign In or Register to comment.