Skip to content

Commit

Permalink
[iOS] Fix ContentPage BackgroundImageSource not working (#17789)
Browse files Browse the repository at this point in the history
* Fix ContentPage BackgroundImageSource not working on iOS/Catalyst

* Adding UI Test

* Updated screenshots

* Updated screenshot

* Updated test

* Created iOS PageExtensions

* Enabled test for all the platforms

* Add pending snapshots

* Changed iOS PageExtensions to internal
  • Loading branch information
jsuarezruiz committed Nov 3, 2023
1 parent 00f39ee commit 5dcb340
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 7 deletions.
@@ -0,0 +1,13 @@
<?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.Issue17789"
BackgroundImageSource="oasis.jpg">
<StackLayout>
<Label
AutomationId="WaitForStubControl"
Text="Issue 17789"
VerticalOptions="Center"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
@@ -0,0 +1,15 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 17789, "ContentPage BackgroundImageSource not working", PlatformAffected.iOS)]
public partial class Issue17789 : ContentPage
{
public Issue17789()
{
InitializeComponent();
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue17789.cs
@@ -0,0 +1,22 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue17789 : _IssuesUITest
{
public Issue17789(TestDevice device) : base(device)
{
}

public override string Issue => "ContentPage BackgroundImageSource not working";

[Test]
public void ContentPageBackgroundImageSourceWorks()
{
App.WaitForElement("WaitForStubControl");
VerifyScreenshot();
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 5 additions & 7 deletions src/Core/src/Handlers/Page/PageHandler.iOS.cs
Expand Up @@ -23,20 +23,18 @@ protected override ContentView CreatePlatformView()

public static void MapBackground(IPageHandler handler, IContentView page)
{
if (handler is IPlatformViewHandler invh && invh.ViewController is not null)
if (handler is IPlatformViewHandler platformViewHandler && platformViewHandler.ViewController is not null)
{
invh.ViewController.View?.UpdateBackground(page);
var provider = handler.GetRequiredService<IImageSourceServiceProvider>();
platformViewHandler.ViewController.View?.UpdateBackground(page, provider);
}
}

public static void MapTitle(IPageHandler handler, IContentView page)
{
if (handler is IPlatformViewHandler invh && invh.ViewController is not null)
if (handler is IPlatformViewHandler platformViewHandler && platformViewHandler.ViewController is not null)
{
if (page is ITitledElement titled)
{
invh.ViewController.Title = titled.Title;
}
platformViewHandler.ViewController.UpdateTitle(page);
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/Core/src/Platform/iOS/PageExtensions.cs
@@ -0,0 +1,23 @@
using UIKit;

namespace Microsoft.Maui.Platform
{
internal static class PageExtensions
{
public static void UpdateTitle(this UIViewController viewController, IContentView page)
{
if (page is not ITitledElement titled)
return;

viewController.Title = titled.Title;
}

public static void UpdateBackground(this UIView platformView, IContentView page, IImageSourceServiceProvider? provider)
{
if (page.Background is ImageSourcePaint image)
platformView.UpdateBackgroundImageSourceAsync(image.ImageSource, provider).FireAndForget();
else
platformView.UpdateBackground(page);
}
}
}

0 comments on commit 5dcb340

Please sign in to comment.