summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-06-08 09:47:00 -0600
committerRui Marinho <me@ruimarinho.net>2016-06-08 16:47:00 +0100
commit4146d617a038e3dd748d6f42796dd791f28145b0 (patch)
tree72ed572473fd691934ba4a60b64d4085057775fe
parentd1b8ac5c940c6f2ae6798337be53ececd60992f7 (diff)
downloadxamarin-forms-4146d617a038e3dd748d6f42796dd791f28145b0.tar.gz
xamarin-forms-4146d617a038e3dd748d6f42796dd791f28145b0.tar.bz2
xamarin-forms-4146d617a038e3dd748d6f42796dd791f28145b0.zip
Remove hard-coded image sizes for button images (#202)
-rw-r--r--Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla27417Xaml.xaml18
-rw-r--r--Xamarin.Forms.Platform.WP8/ButtonRenderer.cs35
-rw-r--r--Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs28
3 files changed, 53 insertions, 28 deletions
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla27417Xaml.xaml b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla27417Xaml.xaml
index ab652b30..d050524e 100644
--- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla27417Xaml.xaml
+++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla27417Xaml.xaml
@@ -2,14 +2,14 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Forms.Controls.Issues.Bugzilla27417Xaml">
- <StackLayout Spacing="10">
+ <StackLayout Spacing="10" HorizontalOptions="Center" >
- <Button BackgroundColor="Color.Gray" Text="Click Me"></Button>
- <Button BackgroundColor="Color.Gray" Image="coffee.png"></Button>
- <Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me"></Button>
- <Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me" ContentLayout="Top,10"></Button>
- <Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me" ContentLayout="Bottom,10"></Button>
- <Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me" ContentLayout="Right"></Button>
-
- </StackLayout>
+ <Button BackgroundColor="Color.Gray" Text="Click Me"></Button>
+ <Button BackgroundColor="Color.Gray" Image="coffee.png"></Button>
+ <Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me"></Button>
+ <Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me" ContentLayout="Top,10"></Button>
+ <Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me" ContentLayout="Bottom,10"></Button>
+ <Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me" ContentLayout="Right"></Button>
+
+ </StackLayout>
</ContentPage> \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.WP8/ButtonRenderer.cs b/Xamarin.Forms.Platform.WP8/ButtonRenderer.cs
index a75173b6..575de4f3 100644
--- a/Xamarin.Forms.Platform.WP8/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.WP8/ButtonRenderer.cs
@@ -4,6 +4,7 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
+using Xamarin.Forms.Internals;
using WButton = System.Windows.Controls.Button;
using WImage = System.Windows.Controls.Image;
using WThickness = System.Windows.Thickness;
@@ -91,15 +92,21 @@ namespace Xamarin.Forms.Platform.WinPhone
return;
}
+ var bmp = new BitmapImage(new Uri("/" + elementImage.File, UriKind.Relative));
+
var image = new WImage
{
- Source = new BitmapImage(new Uri("/" + elementImage.File, UriKind.Relative)),
- Width = 30,
- Height = 30,
+ Source = bmp,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
};
+ bmp.ImageOpened += (sender, args) => {
+ image.Width = bmp.PixelWidth;
+ image.Height = bmp.PixelHeight;
+ Element.InvalidateMeasureInternal(InvalidationTrigger.RendererReady);
+ };
+
// No text, just the image
if (string.IsNullOrEmpty(text))
{
@@ -108,10 +115,13 @@ namespace Xamarin.Forms.Platform.WinPhone
}
// Both image and text, so we need to build a container for them
- var layout = Element.ContentLayout;
+ Control.Content = CreateContentContainer(Element.ContentLayout, image, text);
+ }
+
+ static StackPanel CreateContentContainer(Button.ButtonContentLayout layout, WImage image, string text)
+ {
var container = new StackPanel();
- var textBlock = new TextBlock
- {
+ var textBlock = new TextBlock {
Text = text,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
@@ -127,26 +137,31 @@ namespace Xamarin.Forms.Platform.WinPhone
case Button.ButtonContentLayout.ImagePosition.Top:
container.Orientation = Orientation.Vertical;
image.Margin = new WThickness(0, 0, 0, spacing);
+ container.Children.Add(image);
+ container.Children.Add(textBlock);
break;
case Button.ButtonContentLayout.ImagePosition.Bottom:
container.Orientation = Orientation.Vertical;
image.Margin = new WThickness(0, spacing, 0, 0);
+ container.Children.Add(textBlock);
+ container.Children.Add(image);
break;
case Button.ButtonContentLayout.ImagePosition.Right:
container.Orientation = Orientation.Horizontal;
image.Margin = new WThickness(spacing, 0, 0, 0);
+ container.Children.Add(textBlock);
+ container.Children.Add(image);
break;
default:
// Defaults to image on the left
container.Orientation = Orientation.Horizontal;
image.Margin = new WThickness(0, 0, spacing, 0);
+ container.Children.Add(image);
+ container.Children.Add(textBlock);
break;
}
- container.Children.Add(image);
- container.Children.Add(textBlock);
-
- Control.Content = container;
+ return container;
}
void UpdateFont()
diff --git a/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs b/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs
index 04c39caf..018fdd4d 100644
--- a/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs
+++ b/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs
@@ -4,6 +4,7 @@ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
+using Xamarin.Forms.Internals;
using WThickness = Windows.UI.Xaml.Thickness;
using WButton = Windows.UI.Xaml.Controls.Button;
using WImage = Windows.UI.Xaml.Controls.Image;
@@ -135,13 +136,20 @@ namespace Xamarin.Forms.Platform.WinRT
return;
}
+ var bmp = new BitmapImage(new Uri("ms-appx:///" + elementImage.File));
+
var image = new WImage
{
- Source = new BitmapImage(new Uri("ms-appx:///" + elementImage.File)),
- Width = 30,
- Height = 30,
+ Source = bmp,
VerticalAlignment = VerticalAlignment.Center,
- HorizontalAlignment = HorizontalAlignment.Center
+ HorizontalAlignment = HorizontalAlignment.Center,
+ Stretch = Stretch.Uniform
+ };
+
+ bmp.ImageOpened += (sender, args) => {
+ image.Width = bmp.PixelWidth;
+ image.Height = bmp.PixelHeight;
+ Element.InvalidateMeasureInternal(InvalidationTrigger.RendererReady);
};
// No text, just the image
@@ -152,10 +160,13 @@ namespace Xamarin.Forms.Platform.WinRT
}
// Both image and text, so we need to build a container for them
- var layout = Element.ContentLayout;
+ Control.Content = CreateContentContainer(Element.ContentLayout, image, text);
+ }
+
+ static StackPanel CreateContentContainer(Button.ButtonContentLayout layout, WImage image, string text)
+ {
var container = new StackPanel();
- var textBlock = new TextBlock
- {
+ var textBlock = new TextBlock {
Text = text,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center
@@ -195,8 +206,7 @@ namespace Xamarin.Forms.Platform.WinRT
break;
}
- Control.Content = container;
-
+ return container;
}
void UpdateFont()