diff --git a/samples/ControlGallery/Views/Controls/Labels.razor b/samples/ControlGallery/Views/Controls/Labels.razor
new file mode 100644
index 00000000..2253bf57
--- /dev/null
+++ b/samples/ControlGallery/Views/Controls/Labels.razor
@@ -0,0 +1,32 @@
+@page "/controls/labels"
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+@code {
+ Random _random = new();
+
+ string RandomText => _random.Next().ToString();
+}
diff --git a/samples/ControlGallery/Views/PlaygroundList.razor b/samples/ControlGallery/Views/PlaygroundList.razor
index 144ebc12..3f568d1b 100644
--- a/samples/ControlGallery/Views/PlaygroundList.razor
+++ b/samples/ControlGallery/Views/PlaygroundList.razor
@@ -6,6 +6,7 @@
+
diff --git a/src/Microsoft.MobileBlazorBindings.Core/TextSpanContainer.cs b/src/Microsoft.MobileBlazorBindings.Core/TextSpanContainer.cs
index 9aa68e5a..c65122d2 100644
--- a/src/Microsoft.MobileBlazorBindings.Core/TextSpanContainer.cs
+++ b/src/Microsoft.MobileBlazorBindings.Core/TextSpanContainer.cs
@@ -42,9 +42,9 @@ public string GetUpdatedText(int index, string text)
}
_textSpans[index] = text;
- var allText = string.Join(string.Empty, _textSpans);
+ var allText = string.Concat(_textSpans);
return TrimWhitespace
- ? allText?.Trim()
+ ? allText.Trim()
: allText;
}
}
diff --git a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/LabelHandler.cs b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/LabelHandler.cs
index ed1f1de9..43b403a1 100644
--- a/src/Microsoft.MobileBlazorBindings/Elements/Handlers/LabelHandler.cs
+++ b/src/Microsoft.MobileBlazorBindings/Elements/Handlers/LabelHandler.cs
@@ -1,13 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
+using Microsoft.MobileBlazorBindings.Core;
+using System;
using System.Diagnostics;
using MC = Microsoft.Maui.Controls;
namespace Microsoft.MobileBlazorBindings.Elements.Handlers
{
- public partial class LabelHandler : ViewHandler, IMauiContainerElementHandler
+ public partial class LabelHandler : ViewHandler, IMauiContainerElementHandler, IHandleChildContentText
{
+ private readonly TextSpanContainer _textSpanContainer = new();
+
public virtual void AddChild(MC.Element child, int physicalSiblingIndex)
{
var childAsSpan = child as MC.Span;
@@ -47,6 +51,21 @@ public virtual void RemoveChild(MC.Element child)
formattedString.Spans.Remove(childAsSpan);
}
+ public void HandleText(int index, string text)
+ {
+ if (LabelControl.FormattedText != null)
+ {
+ if (!string.IsNullOrWhiteSpace(text))
+ {
+ throw new InvalidOperationException("Cannot use both string content and Spans for Label.");
+ }
+ }
+ else
+ {
+ LabelControl.Text = _textSpanContainer.GetUpdatedText(index, text);
+ }
+ }
+
private MC.FormattedString GetFormattedString()
{
if (LabelControl.FormattedText == null)