Skip to content

Commit

Permalink
[Android] Fix RadioButton using ContentPresenter not rendering (#25947)
Browse files Browse the repository at this point in the history
* Return ContentAsString when a template isn't specified

* - fix for iOS and Windows

* - fix

* Added snapshots

* Updated sample

* More snapshots

---------

Co-authored-by: Shane Neuville <[email protected]>
  • Loading branch information
jsuarezruiz and PureWeen authored Nov 21, 2024
1 parent 5b5aaa5 commit 59ce48d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Controls/src/Core/RadioButton/RadioButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,17 @@ public string ContentAsString()

Font ITextStyle.Font => this.ToFont();

#if ANDROID
object IContentView.Content => ContentAsString();
#if ANDROID
object IContentView.Content
{
get
{
if (ResolveControlTemplate() == null)
return ContentAsString();

return Content;
}
}
#endif

IView IContentView.PresentedContent => ((this as IControlTemplated).TemplateRoot as IView) ?? (Content as IView);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25887.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue25887"
Title="Issue 25887">
<ContentPage.Resources>

<ControlTemplate x:Key="RadioButtonTemplate">
<ContentPresenter />
</ControlTemplate>

<Style x:Key="RadioButtonStyle" TargetType="RadioButton">
<Setter Property="MaximumWidthRequest" Value="100" />
<Setter Property="HorizontalOptions" Value="Start" />
<Setter Property="VerticalOptions" Value="Start" />
<Setter Property="ControlTemplate"
Value="{StaticResource RadioButtonTemplate}" />
</Style>

</ContentPage.Resources>
<StackLayout
Padding="12">
<Label
Text="Using ControlTemplate" />
<StackLayout
RadioButtonGroup.GroupName="template">
<RadioButton
IsChecked="True"
AutomationId="RadioButtonTemplate1"
Value="Template 1"
Style="{StaticResource RadioButtonStyle}">
<RadioButton.Content>
<StackLayout>
<Image
Source="coffee.png"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label
AutomationId="Success"
Text="Template 1"
HorizontalOptions="Center"
VerticalOptions="End" />
</StackLayout>
</RadioButton.Content>
</RadioButton>
<RadioButton
AutomationId="RadioButtonTemplate2"
Value="Template 2"
Style="{StaticResource RadioButtonStyle}">
<RadioButton.Content>
<StackLayout>
<Image
Source="coffee.png"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label
Text="Template 2"
HorizontalOptions="Center"
VerticalOptions="End" />
</StackLayout>
</RadioButton.Content>
</RadioButton>
<RadioButton
AutomationId="RadioButtonTemplate3"
Value="Template 3"
Style="{StaticResource RadioButtonStyle}">
<RadioButton.Content>
<StackLayout>
<Image
Source="coffee.png"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label
Text="Template 3"
HorizontalOptions="Center"
VerticalOptions="End" />
</StackLayout>
</RadioButton.Content>
</RadioButton>
<RadioButton
AutomationId="RadioButtonTemplate4"
Value="Template 4"
Style="{StaticResource RadioButtonStyle}">
<RadioButton.Content>
<StackLayout>
<Image
Source="coffee.png"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label
Text="Template 4"
HorizontalOptions="Center"
VerticalOptions="End" />
</StackLayout>
</RadioButton.Content>
</RadioButton>
</StackLayout>
</StackLayout>
</ContentPage>
10 changes: 10 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25887.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 25887, "ContentPresenter just rendering component string in .Net9", PlatformAffected.Android)]
public partial class Issue25887 : ContentPage
{
public Issue25887()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue25887 : _IssuesUITest
{
public Issue25887(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "ContentPresenter just rendering component string in .Net9";

[Test]
[Category(UITestCategories.RadioButton)]
public void RadioButtonContentNotRendering()
{
App.WaitForElement("RadioButtonTemplate1");
VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 59ce48d

Please sign in to comment.