Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): PersonPicture late binding #18587

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2374,6 +2374,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 @@ -7163,6 +7167,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.
ajpinedam marked this conversation as resolved.
Show resolved Hide resolved

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)
ajpinedam marked this conversation as resolved.
Show resolved Hide resolved
{
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
jeromelaban marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading