diff --git a/src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs b/src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs index 28a3bc2b3abb..25de895d29be 100644 --- a/src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs +++ b/src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs @@ -70,6 +70,18 @@ internal static IMauiHandlersCollection AddControlsHandlers(this IMauiHandlersCo handlersCollection.AddHandler(); handlersCollection.AddHandler(); #endif +#if ANDROID + if (RuntimeFeature.IsMaterial3Enabled) + { + handlersCollection.AddHandler(); + } + else + { + handlersCollection.AddHandler(); + } +#else + handlersCollection.AddHandler(); +#endif #if ANDROID if (RuntimeFeature.IsMaterial3Enabled) { diff --git a/src/Controls/src/Core/Label/Label.Android.cs b/src/Controls/src/Core/Label/Label.Android.cs index f9b74b9712fa..ec2310122548 100644 --- a/src/Controls/src/Core/Label/Label.Android.cs +++ b/src/Controls/src/Core/Label/Label.Android.cs @@ -10,26 +10,45 @@ namespace Microsoft.Maui.Controls public partial class Label { MauiTextView _mauiTextView; + MauiMaterialTextView _mauiMaterialTextView; private protected override void OnHandlerChangedCore() { base.OnHandlerChangedCore(); - if (Handler != null) + // Detach from previous views + DetachLayoutChanged(); + + if (Handler is not ILabelHandler { PlatformView: var platformView }) + { + return; + } + + switch (platformView) { - if (Handler is LabelHandler labelHandler && labelHandler.PlatformView is MauiTextView mauiTextView) - { - _mauiTextView = mauiTextView; + case MauiTextView textView: + _mauiTextView = textView; _mauiTextView.LayoutChanged += OnLayoutChanged; - } + break; + case MauiMaterialTextView materialTextView: + _mauiMaterialTextView = materialTextView; + _mauiMaterialTextView.LayoutChanged += OnLayoutChanged; + break; } - else + } + + void DetachLayoutChanged() + { + if (_mauiTextView is not null) { - if (_mauiTextView != null) - { - _mauiTextView.LayoutChanged -= OnLayoutChanged; - _mauiTextView = null; - } + _mauiTextView.LayoutChanged -= OnLayoutChanged; + _mauiTextView = null; + } + + if (_mauiMaterialTextView is not null) + { + _mauiMaterialTextView.LayoutChanged -= OnLayoutChanged; + _mauiMaterialTextView = null; } } @@ -54,19 +73,25 @@ public static void MapMaxLines(ILabelHandler handler, Label label) void OnLayoutChanged(object sender, LayoutChangedEventArgs args) { if (Handler is not ILabelHandler labelHandler) + { return; + } var platformView = labelHandler.PlatformView; var virtualView = labelHandler.VirtualView as Label; if (platformView == null || virtualView == null) + { return; + } var text = virtualView.FormattedText; // don't attempt to layout if this is not a formatted text WITH some text if (virtualView.TextType != TextType.Text || text?.Spans == null || text.Spans.Count == 0) + { return; + } var spannableString = virtualView.ToSpannableString(); diff --git a/src/Controls/src/Core/Label/Label.cs b/src/Controls/src/Core/Label/Label.cs index d815cb41bf41..3521cb8dc192 100644 --- a/src/Controls/src/Core/Label/Label.cs +++ b/src/Controls/src/Core/Label/Label.cs @@ -15,7 +15,6 @@ namespace Microsoft.Maui.Controls /// A that displays text. [ContentProperty(nameof(Text))] [DebuggerDisplay("{GetDebuggerDisplay(), nq}")] - [ElementHandler] public partial class Label : View, IFontElement, ITextElement, ITextAlignmentElement, ILineHeightElement, IElementConfiguration