diff options
author | E.Z. Hart <hartez@gmail.com> | 2016-03-22 17:16:53 -0600 |
---|---|---|
committer | E.Z. Hart <hartez@gmail.com> | 2016-03-30 16:22:35 -0600 |
commit | 17e2a4b94d5105b53c5875d9416d043f14313305 (patch) | |
tree | 6f03e5d6f562f230ed79d2e820327fe0bddc5069 /Xamarin.Forms.Platform.WinRT | |
parent | b3a64fadb3951d4e5e65b5f000a5f930772c01ed (diff) | |
download | xamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.tar.gz xamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.tar.bz2 xamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.zip |
Add options for specifying layout of button text/image content
Also make the layout and layout defaults consistent across platforms
Diffstat (limited to 'Xamarin.Forms.Platform.WinRT')
-rw-r--r-- | Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs | 79 |
1 files changed, 66 insertions, 13 deletions
diff --git a/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs b/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs index 60ee6418..bcba31cc 100644 --- a/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs +++ b/Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs @@ -117,25 +117,78 @@ namespace Xamarin.Forms.Platform.WinRT void UpdateContent() { - if (Element.Image != null) + var text = Element.Text; + var elementImage = Element.Image; + + // No image, just the text + if (elementImage == null) { - var panel = new StackPanel { Orientation = Orientation.Horizontal }; + Control.Content = text; + return; + } - var image = new WImage { Source = new BitmapImage(new Uri("ms-appx:///" + Element.Image.File)), Width = 30, Height = 30, Margin = new WThickness(0, 0, 20, 0) }; - panel.Children.Add(image); - image.ImageOpened += (sender, args) => { ((IButtonController)Element).NativeSizeChanged(); }; + var image = new WImage + { + Source = new BitmapImage(new Uri("ms-appx:///" + elementImage.File)), + Width = 30, + Height = 30, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Center + }; + + // No text, just the image + if (string.IsNullOrEmpty(text)) + { + Control.Content = image; + return; + } - if (Element.Text != null) - { - panel.Children.Add(new TextBlock { Text = Element.Text }); - } + // Both image and text, so we need to build a container for them + var layout = Element.ContentLayout; + var container = new StackPanel(); + var textBlock = new TextBlock + { + Text = text, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Center + }; - Control.Content = panel; - } - else + var spacing = layout.Spacing; + + container.HorizontalAlignment = HorizontalAlignment.Center; + container.VerticalAlignment = VerticalAlignment.Center; + + switch (layout.Position) { - Control.Content = Element.Text; + 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; } + + Control.Content = container; + } void UpdateFont() |