Skip to content

Commit

Permalink
[iOS] Frames' content update (dotnet#20501)
Browse files Browse the repository at this point in the history
* [iOS] Fixed updating frame's content (dotnet#19127)

* Added a UiTest (dotnet#19127)

* Code refactor

---------

Co-authored-by: Javier Suárez <[email protected]>
  • Loading branch information
kubaflo and jsuarezruiz authored Nov 2, 2024
1 parent 1ddd9a0 commit e8c96c5
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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.Issue19127"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
<VerticalStackLayout
VerticalOptions="Center">
<Button Text="{Binding IsCameraEnabled, StringFormat='Toggle Camera View ({0})'}"
Command="{Binding EnableCamera}"
AutomationId="button" />

<Frame x:Name="frame"
HeightRequest="191">
<Frame.Triggers>
<DataTrigger TargetType="Frame" Binding="{Binding IsCameraEnabled}" Value="False">
<Setter Property="Content">
<Grid BackgroundColor="Gray">
<Label AutomationId="label1" Text="Camera is Disabled" TextColor="White" />
</Grid>
</Setter>
</DataTrigger>
<DataTrigger TargetType="Frame" Binding="{Binding IsCameraEnabled}" Value="True">
<Setter Property="Content">
<Grid BackgroundColor="HotPink">
<Label AutomationId="label2" Text="Camera is Enabled" TextColor="White" />
</Grid>
</Setter>
</DataTrigger>
</Frame.Triggers>
</Frame>
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.ComponentModel;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 19127, "Triggers are not working on Frame control", PlatformAffected.iOS)]
public partial class Issue19127 : ContentPage
{
public Issue19127()
{
InitializeComponent();
BindingContext = new Issue19127Settings();
}
}

public class Issue19127Settings : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public Command EnableCamera { get; set; }

private bool _isCameraEnabled;
public bool IsCameraEnabled
{
get { return _isCameraEnabled; }
set
{
_isCameraEnabled = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsCameraEnabled)));
}
}

public Issue19127Settings()
{
IsCameraEnabled = false;
EnableCamera = new Command(() => ToggleEnableCamera());
}

private void ToggleEnableCamera()
{
IsCameraEnabled = !_isCameraEnabled;
}
}
}
33 changes: 23 additions & 10 deletions src/Controls/src/Core/Compatibility/Handlers/iOS/FrameRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ namespace Microsoft.Maui.Controls.Handlers.Compatibility
public class FrameRenderer : VisualElementRenderer<Frame>
{
public static IPropertyMapper<Frame, FrameRenderer> Mapper
= new PropertyMapper<Frame, FrameRenderer>(VisualElementRendererMapper);
= new PropertyMapper<Frame, FrameRenderer>(VisualElementRendererMapper)
{
[VisualElement.BackgroundColorProperty.PropertyName] = (h, _) => h.SetupLayer(),
[VisualElement.BackgroundProperty.PropertyName] = (h, _) => h.SetupLayer(),
[Microsoft.Maui.Controls.Frame.BorderColorProperty.PropertyName] = (h, _) => h.SetupLayer(),
[Microsoft.Maui.Controls.Frame.CornerRadiusProperty.PropertyName] = (h, _) => h.SetupLayer(),
[Microsoft.Maui.Controls.Frame.IsClippedToBoundsProperty.PropertyName] = (h, _) => h.SetupLayer(),
[VisualElement.IsVisibleProperty.PropertyName] = (h, _) => h.SetupLayer(),
[Controls.Frame.HasShadowProperty.PropertyName] = (h, _) => h.UpdateShadow(),
[Microsoft.Maui.Controls.Frame.ContentProperty.PropertyName] = (h, _) => h.UpdateContent(),
};

public static CommandMapper<Frame, FrameRenderer> CommandMapper
= new CommandMapper<Frame, FrameRenderer>(VisualElementRendererCommandMapper);
Expand Down Expand Up @@ -60,16 +70,19 @@ protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
}

if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName ||
e.PropertyName == VisualElement.BackgroundProperty.PropertyName ||
e.PropertyName == Microsoft.Maui.Controls.Frame.BorderColorProperty.PropertyName ||
e.PropertyName == Microsoft.Maui.Controls.Frame.CornerRadiusProperty.PropertyName ||
e.PropertyName == Microsoft.Maui.Controls.Frame.IsClippedToBoundsProperty.PropertyName ||
e.PropertyName == VisualElement.IsVisibleProperty.PropertyName)
SetupLayer();
else if (e.PropertyName == Controls.Frame.HasShadowProperty.PropertyName)
UpdateShadow();
void UpdateContent()
{
_actualView.ClearSubviews();

var content = Element?.Content;

if (content == null || MauiContext == null)
return;

var platformView = content.ToPlatform(MauiContext);
_actualView.AddSubview(platformView);
}

public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
Expand Down
30 changes: 30 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue19127.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue19127 : _IssuesUITest
{
public override string Issue => "Triggers are not working on Frame control";

public Issue19127(TestDevice device) : base(device)
{
}

[Test]
public void ContentOfFrameShouldChange()
{
_ = App.WaitForElement("button");

var textBeforeClick = App.FindElement("label1").GetText();

App.Click("button");

var textAfterClick = App.FindElement("label2").GetText();

Assert.AreEqual(textBeforeClick, "Camera is Disabled");
Assert.AreEqual(textAfterClick, "Camera is Enabled");
}
}
}

0 comments on commit e8c96c5

Please sign in to comment.