Repository: unoplatform/uno
File: src/Uno.UI.Composition/Composition/ContainerVisual.skia.cs (C#)

Code before:
	private List<Visual>? _childrenInRenderOrder;
	private bool _hasCustomRenderOrder;
	private int? _subtreeVisualCount;

	private (Rect rect, bool isAncestorClip)? _layoutClip;

	private GCHandle _gcHandle;

	partial void InitializePartial()
	{
		Children.CollectionChanged += (s, e) =>
		{
			IsChildrenRenderOrderDirty = true;

			var parent = this;
			while (parent is not null && parent._subtreeVisualCount is not null)
			{
				parent._subtreeVisualCount = null;
				parent = parent.Parent;
			}

			InvalidateParentChildrenPicture(true);

			// ensures a frame is drawn after, because otherwise, sometimes
			// removing a child from a fixed sized parent can leave a visual artifact
			CompositionTarget?.RequestNewFrame();

			if (e.Action is NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Reset
				&& e.OldItems is not null)
			{
				foreach (var i in e.OldItems)
				{
					if (i is CompositionObject compositionObject)
					{
						compositionObject.StopAllAnimations();
					}
				}
			}
		};

		_gcHandle = GCHandle.Alloc(this, GCHandleType.Weak);
		Handle = GCHandle.ToIntPtr(_gcHandle);
	}

	internal IntPtr Handle { get; private set; }

	internal WeakReference? Owner { get; set; }

	internal string? OwnerDebugName => Owner?.Target?.GetType().Name;

	/// <summary>

Review comment (suggestion):
Definitely optional, but this could probably use more explaining. Something like "We need to force a redraw because at this point it's not necessarily true that a visual in the composition tree was changed, only that it was added/removed, so it's possible that no InvalidatePaint() calls were fired in response to this change, so we need to force a new frame even though no paint invalidations happened just so that already-clean added/removed visuals are reflected in the UI".

Code after:
	private List<Visual>? _childrenInRenderOrder;
	private bool _hasCustomRenderOrder;
	private int? _subtreeVisualCount;

	private (Rect rect, bool isAncestorClip)? _layoutClip;

	private GCHandle _gcHandle;

	partial void InitializePartial()
	{
		Children.CollectionChanged += (s, e) =>
		{
			IsChildrenRenderOrderDirty = true;

			var parent = this;
			while (parent is not null && parent._subtreeVisualCount is not null)
			{
				parent._subtreeVisualCount = null;
				parent = parent.Parent;
			}

			InvalidateParentChildrenPicture(true);

			// We need to force a redraw because at this point it's not necessarily true that
			// a visual in the composition tree was changed, only that it was added/removed,
			// so it's possible that no InvalidatePaint() calls were fired in response to this change, 
			// so we need to force a new frame even though no paint invalidations happened just so that
			// already-clean added/removed visuals are reflected in the UI
			CompositionTarget?.RequestNewFrame();

			if (e.Action is NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Reset
				&& e.OldItems is not null)
			{
				foreach (var i in e.OldItems)
				{
					if (i is CompositionObject compositionObject)
					{
						compositionObject.StopAllAnimations();
					}
				}
			}
		};

		_gcHandle = GCHandle.Alloc(this, GCHandleType.Weak);
		Handle = GCHandle.ToIntPtr(_gcHandle);
	}

	internal IntPtr Handle { get; private set; }

	internal WeakReference? Owner { get; set; }