The component lifecycles
The lifecycle of a component it is very simple. A component can be:
Dettached: The component object is created but is not attached to the tree. TVision2 don’t care about a component until it is attached to the tree.
Attached & Running: The component is running and it is attached to the tree. When a component is attached participates from the main loop through the methods
UpdateandDraw, until you remove the component from the tree.
Events and notifications when attaching a component
When a component is attached to the tree following events are triggered:
- The
OnComponentMountedaction from the component metadata is called. - The
ComponentAddedevent from the ComponentTree is triggered - The
AfterAddActionspecified when creating the component (if any) is called - The
TreeUpdatedevent from the ComponentTree is triggered
Remember that adding a component to the tree is an asynchronous operation and many components can be added in a single “add operation”. Steps 1,2 and 3 happen once per component and step 4 happen only once per add operation (when all components are added).
When dettaching a component following happens:
- The
OnComponentWillBeUnmountedaction from the component metadata is invoked. At this point the component can abort the dettach operation. - The
OnComponentUnmountedaction from the component metadata is invoked - The
ComponentRemovedevent from the tree is triggered - The
TreeUpdatedevent from the tree is triggered
Like adding components, removing is an asynchronous operation. Steps 1, 2 & 3 happen once per each component, and step 4 only once when all components in the “remove operation” are removed from the tree.
Dettaching a parent component
When a component that have childs is dettached, all their childs are dettached too. First the childs are dettached and lastly the parent component is.
If any of the childs aborts the dettaching operation (through the
OnComponentWillBeUnmountedaction) no component is deleted.
Using the Metadata actions of the component lifecycle
When creating the component you can add the actions OnComponentMounted, OnComponentUnmounted and OnComponentWillBeUnmounted:
var cmp = new TvComponent<string>("hello", "label",
cfg =>
{
cfg.WhenComponentMounted(ctx => ComponentMounted(ctx));
});
);
This code creates a component holding an string, and adds the ComponentMounted action. When adding an action you provide a deletage that will be invoked.
You can use following methods:
WhenComponentMounted(Action<ComponentMoutingContext> mountAction);
WhenComponentUnmounted(Action<ComponentMoutingContext> unmountAction);
WhenComponentWillbeUnmounted(Action<ComponentMountingCancellableContext> unmountAction);
WhenChildMounted(Action<ChildComponentMoutingContext> childMountAction);
WhenChildUnmounted(Action<ChildComponentMoutingContext> childUndmountAction);
WhenComponentMounted(OnComponentMountedaction): Invoked when the component is mountedWhenComponentUnmounted(OnComponentUnmountedaction): Invoked then the component is unmountedWhenComponentWillbeUnmounted(OnComponentWillbeUnmountedaction): Invoked when the component is about to be invoked giving the chance to the component to abort the operationWhenChildMounted(OnChildMountedActionaction): Invoked when a child component is mountedWhenChildUnmounted(OnChildMountedUnmountedaction): Invoked when a child component is unmounted.
When adding a component as a child, it is possible to use an option to avoid the
OnChildMountedActionto be called.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.