Skip to content

Commit

Permalink
[Windows] Correctly size the ImageButton BitmapImage (#6663)
Browse files Browse the repository at this point in the history
* Correctly size the Windows ImageButton BitmapImage

* Update logic to set ImageButton image size

* Remove unnecessary changes
  • Loading branch information
jsuarezruiz authored May 11, 2022
1 parent 83c43b3 commit 6ff3e58
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
Style="{StaticResource Headline}"/>
<ImageButton
HorizontalOptions="Center"
WidthRequest="200"

WidthRequest="200"
Clicked="OnImageButtonClicked"
Aspect="AspectFit"
Background="Green"
Expand Down Expand Up @@ -84,6 +83,15 @@
Source="cog.png"
CornerRadius="10"
BackgroundColor="Purple"/>
<Label
Text="Custom Size"
Style="{StaticResource Headline}"/>
<ImageButton
x:Name="ResizeImageButton"
Source="dotnet_bot.png"
WidthRequest="40"
HeightRequest="40"
Clicked="OnResizeImageButtonClicked"/>
</VerticalStackLayout>
</ScrollView>
</views:BasePage.Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ void OnImageButtonClicked(object sender, EventArgs e)
_clickTotal += 1;
InfoLabel.Text = $"{_clickTotal} ImageButton click{(_clickTotal == 1 ? "" : "s")}";
}

void OnResizeImageButtonClicked(object sender, EventArgs e)
{
ResizeImageButton.HeightRequest = 100;
ResizeImageButton.WidthRequest = 100;
}
}

public class ImageButtonPageViewModel : BindableObject
Expand Down
13 changes: 6 additions & 7 deletions src/Core/src/Platform/Windows/ButtonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,14 @@ void OnImageOpened(object sender, RoutedEventArgs e)
{
bitmapImage.ImageOpened -= OnImageOpened;

// check if the image that just loaded is still the current image
// Check if the image that just loaded is still the current image
var actualImageSource = sender as BitmapImage;

if (actualImageSource is not null && nativeImage.Source == actualImageSource)
{
// do the actual resize
var imageSourceSize = actualImageSource.GetImageSourceSize(platformButton);
nativeImage.Width = imageSourceSize.Width;
nativeImage.Height = imageSourceSize.Height;
}
nativeImage.Height = nativeImage.Width = Primitives.Dimension.Unset;

if (platformButton.Parent is FrameworkElement frameworkElement)
frameworkElement.InvalidateMeasure();
};
}
}
Expand Down

0 comments on commit 6ff3e58

Please sign in to comment.