Skip to content
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
18 changes: 15 additions & 3 deletions src/Controls/src/Core/ImageElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ static void OnImageSourceChanged(BindableObject bindable, object oldValue, objec
{
var newSource = (ImageSource)newValue;
var image = (IImageElement)bindable;
if (newSource != null && image != null)

if (newSource is not null && image is not null)
{
newSource.SourceChanged += image.OnImageSourceSourceChanged;
}

ImageSourceChanged(bindable, newSource);
}

Expand All @@ -37,8 +41,16 @@ static void OnImageSourceChanging(BindableObject bindable, object oldValue, obje
var oldSource = (ImageSource)oldValue;
var image = (IImageElement)bindable;

if (oldSource != null && image != null)
oldSource.SourceChanged -= image.OnImageSourceSourceChanged;
if (oldSource is not null)
{
if (image is not null)
{
oldSource.SourceChanged -= image.OnImageSourceSourceChanged;
}

oldSource.Parent = null;
}

ImageSourceChanging(oldSource);
}

Expand Down
13 changes: 12 additions & 1 deletion src/Controls/tests/Core.UnitTests/ImageSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,16 @@ public async Task CancelCompletes()
await ((IStreamImageSource)imageSource).GetStreamAsync();
await imageSource.Cancel(); // This should complete!
}

[Fact]
public void SettingNewImageeSourceClearsParentOnOldImageSource()
{
var image = new Image { Source = "File.png" };
var imageSource = image.Source;
Assert.Equal(image, imageSource.Parent);
image.Source = "File2.png";
Assert.Null(imageSource.Parent);
Assert.Equal(image, image.Source.Parent);
}
}
}
}
Loading