From 27b8530e2da5d8d4b91b8df88c43384718bc4eda Mon Sep 17 00:00:00 2001
From: Jerome Laban <jerome@platform.uno>
Date: Thu, 9 Jan 2025 21:35:34 -0500
Subject: [PATCH] fix(resources): Don't reevaluate all resources on all measure

---
 .../UI/Xaml/FrameworkElement.Layout.crossruntime.cs  | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/Uno.UI/UI/Xaml/FrameworkElement.Layout.crossruntime.cs b/src/Uno.UI/UI/Xaml/FrameworkElement.Layout.crossruntime.cs
index e5c7d1b244a5..c90ae077b946 100644
--- a/src/Uno.UI/UI/Xaml/FrameworkElement.Layout.crossruntime.cs
+++ b/src/Uno.UI/UI/Xaml/FrameworkElement.Layout.crossruntime.cs
@@ -25,6 +25,7 @@ public partial class FrameworkElement
 		private readonly static IEventProvider _trace = Tracing.Get(FrameworkElement.TraceProvider.Id);
 
 		private bool m_firedLoadingEvent;
+		private bool m_requiresResourcesUpdate = true;
 
 		private const double SIZE_EPSILON = 0.05d;
 		private readonly Size MaxSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
@@ -282,11 +283,12 @@ private void InnerMeasureCore(Size availableSize)
 			//if (!bInLayoutTransition)
 			{
 				// Templates should be applied here.
-				InvokeApplyTemplate(out _);
+				InvokeApplyTemplate(out var addedVisual);
 
 				// TODO: BEGIN Uno specific
-				if (this is Control thisAsControl)
+				if (m_requiresResourcesUpdate && this is Control thisAsControl)
 				{
+					m_requiresResourcesUpdate = false;
 					// Update bindings to ensure resources defined
 					// in visual parents get applied.
 					this.UpdateResourceBindings();
@@ -991,6 +993,10 @@ internal override void EnterImpl(EnterParams @params, int depth)
 		{
 			var core = this.GetContext();
 
+			// ---------- Uno-specific BEGIN ----------
+			m_requiresResourcesUpdate = true;
+			// ---------- Uno-specific END ----------
+
 			//if (@params.IsLive && @params.CheckForResourceOverrides == false)
 			//{
 			//    var resources = GetResourcesNoCreate();
@@ -1066,7 +1072,7 @@ internal override void LeaveImpl(LeaveParams @params)
 			// of properties that are marked with MetaDataPropertyInfoFlags::IsSparse and MetaDataPropertyInfoFlags::IsVisualTreeProperty
 			// are entered as well.
 			// The property we currently know it has an effect is Resources
-			if (Resources is not null)
+			if (TryGetResources() is not null)
 			{
 				// Using ValuesInternal to avoid Enumerator boxing
 				foreach (var resource in Resources.ValuesInternal)