Skip to content

Commit

Permalink
Merge pull request #18587 from ajpinedam/ajpm/fix.ios.person.picture
Browse files Browse the repository at this point in the history
fix(ios): PersonPicture late binding
  • Loading branch information
jeromelaban authored Nov 21, 2024
2 parents 77ab1de + 0cd2e49 commit a7b4a27
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\PersonPictureTests\PersonPictureLateBindingPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\Pivot\Pivot_CustomContent_Automated.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -7182,6 +7186,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\PersonPictureTests\PersonPicturePage.xaml.cs">
<DependentUpon>PersonPicturePage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\PersonPictureTests\PersonPictureLateBindingPage.xaml.cs">
<DependentUpon>PersonPictureLateBindingPage.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\Pivot\Pivot_CustomContent_Automated.xaml.cs">
<DependentUpon>Pivot_CustomContent_Automated.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -->
<UserControl
x:Class="MUXControlsTestApp.PersonPictureLateBindingPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MUXControlsTestApp"
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Name="m_MainStackPanel"
Spacing="16">

<PersonPicture ProfilePicture="{Binding ImagePath}"
Height="100"
Width="100"
HorizontalAlignment="Center"
VerticalAlignment="Center" />

<TextBlock Text="{Binding LeName}"
FontSize="22"
HorizontalAlignment="Center"/>
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.ObjectModel;
using MUXControlsTestApp.Utilities;
using Uno.UI.Samples.Controls;
using Windows.ApplicationModel.Contacts;
using System.Threading.Tasks;

namespace MUXControlsTestApp
{
[SampleControlInfo(
"MUX",
"PersonPictureLateBinding",
description: "Shows a PersonPicture control and a Textblock with a name. \n" +
"The PersonPicture control image is set after 1 second delay. \n" +
"The image should be displayed after the delay.",
isManualTest: true)]
#pragma warning disable UXAML0002 // does not explicitly define the Microsoft.UI.Xaml.Controls.UserControl base type in code behind.
public sealed partial class PersonPictureLateBindingPage
#pragma warning restore UXAML0002 // does not explicitly define the Microsoft.UI.Xaml.Controls.UserControl base type in code behind.
{
public string ImagePath { get; } = "ms-appx:///Assets/ingredient2.png";

public string LeName { get; } = "James Bondi";

public PersonPictureLateBindingPage()
{
this.InitializeComponent();
_ = SetImagePathWithDelay();
}

private async Task SetImagePathWithDelay()
{
await Task.Delay(1000);
DataContext = this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,33 @@ void UpdateIfReady()

var templateSettings = TemplateSettings;
templateSettings.ActualInitials = initials;
if (imageSrc != null)

if (imageSrc is not null)
{
var imageBrush = templateSettings.ActualImageBrush;
if (imageBrush == null)
if (templateSettings.ActualImageBrush is ImageBrush imageBrush)
{
imageBrush = new ImageBrush();
imageBrush.Stretch = Stretch.UniformToFill;
templateSettings.ActualImageBrush = imageBrush;
imageBrush.ImageSource = imageSrc;
}
else
{
templateSettings.ActualImageBrush = new ImageBrush()
{
ImageSource = imageSrc,
Stretch = Stretch.UniformToFill
};
}

imageBrush.ImageSource = imageSrc;
}
else
{
templateSettings.ActualImageBrush = null;
}

#if __IOS__
if (templateSettings.ActualImageBrush is ImageBrush brush)
{
brush.ImageOpened += RefreshPhoto;
}
#endif
// If the control is converted to 'Group-mode', we'll clear individual-specific information.
// When IsGroup evaluates to false, we will restore state.
if (IsGroup)
Expand All @@ -228,7 +238,11 @@ void UpdateIfReady()
}
else
{
if (imageSrc != null)
if (imageSrc is not null
#if __IOS__
&& imageSrc.IsOpened
#endif
)
{
VisualStateManager.GoToState(this, "Photo", false);
}
Expand All @@ -245,6 +259,18 @@ void UpdateIfReady()
UpdateAutomationName();
}

#if __IOS__
void RefreshPhoto(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this, "Photo", false);

if (TemplateSettings.ActualImageBrush is { } brush)
{
brush.ImageOpened -= RefreshPhoto;
}
}
#endif

void UpdateBadge()
{
if (BadgeImageSource != null)
Expand Down
5 changes: 5 additions & 0 deletions src/Uno.UI/UI/Xaml/Media/ImageSource.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public static NSUrlSession DefaultSession

static public implicit operator ImageSource(UIImage image) => new ImageSource(image);

/// <summary>
/// Indicates that this source has already been opened (So TryOpenSync will return true!)
/// </summary>
internal bool IsOpened => _imageData.HasData;

private static UIImage? OpenBundleFromString(string? bundle)
{
if (!bundle.IsNullOrWhiteSpace())
Expand Down

0 comments on commit a7b4a27

Please sign in to comment.