Skip to content

Commit fe0d1d9

Browse files
committed
Fix #33829: Don't log warning for View content when ControlTemplate is set
When RadioButton has a ControlTemplate applied, View content IS supported per documentation. The warning should only appear when no ControlTemplate is set. - Modified ContentAsString() to check ResolveControlTemplate() == null before logging - Added unit tests to verify correct warning behavior - Updated XML documentation to reflect correct behavior
1 parent 53900b3 commit fe0d1d9

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

src/Controls/src/Core/RadioButton/RadioButton.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public partial class RadioButton : TemplatedView, IElementConfiguration<RadioBut
2626
/// </summary>
2727
/// <value>The string "Checked".</value>
2828
public const string CheckedVisualState = "Checked";
29-
29+
3030
/// <summary>
3131
/// The visual state name for when the radio button is unchecked.
3232
/// </summary>
@@ -38,13 +38,13 @@ public partial class RadioButton : TemplatedView, IElementConfiguration<RadioBut
3838
/// </summary>
3939
/// <value>The string "Root".</value>
4040
public const string TemplateRootName = "Root";
41-
41+
4242
/// <summary>
4343
/// The name of the checked indicator element in the control template.
4444
/// </summary>
4545
/// <value>The string "CheckedIndicator".</value>
4646
public const string CheckedIndicator = "CheckedIndicator";
47-
47+
4848
/// <summary>
4949
/// The name of the unchecked button element in the control template.
5050
/// </summary>
@@ -686,12 +686,15 @@ static View BuildDefaultTemplate()
686686
/// </summary>
687687
/// <returns>The string representation of the content, or the result of <c>ToString()</c> if content is not a string.</returns>
688688
/// <remarks>
689-
/// If <see cref="Content"/> is a <see cref="View"/>, a warning is logged and the <c>ToString()</c> representation is used instead.
689+
/// If <see cref="Content"/> is a <see cref="View"/> and no <see cref="ControlTemplate"/> is set, a warning is logged
690+
/// and the <c>ToString()</c> representation is used instead. When a ControlTemplate is applied, View content is supported.
690691
/// </remarks>
691692
public string ContentAsString()
692693
{
693694
var content = Content;
694-
if (content is View)
695+
// Only log warning if Content is a View AND no ControlTemplate is set
696+
// When ControlTemplate is set, View content IS supported (per documentation)
697+
if (content is View && ResolveControlTemplate() == null)
695698
{
696699
Application.Current?.FindMauiContext()?.CreateLogger<RadioButton>()?.LogWarning("Warning - {RuntimePlatform} does not support View as the {PropertyName} property of RadioButton; the return value of the ToString() method will be displayed instead.", DeviceInfo.Platform, ContentProperty.PropertyName);
697700
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Linq;
3+
using Xunit;
4+
5+
namespace Microsoft.Maui.Controls.Core.UnitTests
6+
{
7+
/// <summary>
8+
/// Tests for RadioButton ContentAsString() warning behavior.
9+
/// Issue #33829: Warning should not be logged when ControlTemplate is set because
10+
/// View content IS supported in that scenario.
11+
/// </summary>
12+
[Category("RadioButton")]
13+
public class RadioButtonContentAsStringTests : BaseTestFixture
14+
{
15+
public RadioButtonContentAsStringTests()
16+
{
17+
ApplicationExtensions.CreateAndSetMockApplication();
18+
}
19+
20+
protected override void Dispose(bool disposing)
21+
{
22+
if (disposing)
23+
{
24+
Application.ClearCurrent();
25+
}
26+
base.Dispose(disposing);
27+
}
28+
29+
[Fact]
30+
public void ContentAsStringDoesNotLogWarningWhenControlTemplateIsSet()
31+
{
32+
// Arrange: RadioButton with ControlTemplate AND View Content
33+
// This scenario IS supported per documentation, so no warning should be logged
34+
var radioButton = new RadioButton
35+
{
36+
ControlTemplate = RadioButton.DefaultTemplate,
37+
Content = new Label { Text = "Test Label" }
38+
};
39+
40+
// Act
41+
var result = radioButton.ContentAsString();
42+
43+
// Assert: No warning should be logged when ControlTemplate is set
44+
Assert.True(MockApplication.MockLogger.Messages.Count == 0,
45+
"No warning should be logged when ControlTemplate is set. " +
46+
$"Found: {MockApplication.MockLogger.Messages.FirstOrDefault()}");
47+
Assert.NotNull(result);
48+
}
49+
50+
[Fact]
51+
public void ContentAsStringLogsWarningWhenNoControlTemplate()
52+
{
53+
// Arrange: RadioButton without ControlTemplate but with View Content
54+
// This scenario is NOT supported, so warning should be logged
55+
var radioButton = new RadioButton
56+
{
57+
Content = new Label { Text = "Test Label" }
58+
};
59+
60+
// Act
61+
var result = radioButton.ContentAsString();
62+
63+
// Assert: Warning SHOULD be logged when ControlTemplate is null
64+
Assert.Single(MockApplication.MockLogger.Messages);
65+
Assert.Contains("does not support View as the Content property", MockApplication.MockLogger.Messages.First(), StringComparison.Ordinal);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)