[W] Button BG Color & BorderRadius are consistent
authorSamantha Houts <samantha.houts@xamarin.com>
Tue, 29 Mar 2016 22:41:39 +0000 (15:41 -0700)
committerSamantha Houts <samantha.houts@xamarin.com>
Tue, 29 Mar 2016 22:41:39 +0000 (15:41 -0700)
UWP Buttons now respect the BorderRadius.
BackgroundColor no longer extends past the Border.

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla39853.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.WinRT/ButtonRenderer.cs
Xamarin.Forms.Platform.WinRT/FormsButton.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla39853.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla39853.cs
new file mode 100644 (file)
index 0000000..54f4e3e
--- /dev/null
@@ -0,0 +1,78 @@
+using System;
+
+using Xamarin.Forms.CustomAttributes;
+
+namespace Xamarin.Forms.Controls
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Bugzilla, 39853, "BorderRadius ignored on UWP", PlatformAffected.WinRT)]
+       public class Bugzilla39853 : TestContentPage
+       {
+               public class RoundedButton : Xamarin.Forms.Button
+               {
+                       public RoundedButton(int radius)
+                       {
+                               base.BorderRadius = radius;
+                               base.WidthRequest = 2 * radius;
+                               base.HeightRequest = 2 * radius;
+                               HorizontalOptions = LayoutOptions.Center;
+                               VerticalOptions = LayoutOptions.Center;
+                               BackgroundColor = Color.Aqua;
+                               BorderColor = Color.White;
+                               TextColor = Color.Purple;
+                               Text = "YAY";
+                               Image = new FileImageSource { File = "crimson.jpg" };
+                       }
+
+                       public new int BorderRadius
+                       {
+                               get
+                               {
+                                       return base.BorderRadius;
+                               }
+
+                               set
+                               {
+                                       base.WidthRequest = 2 * value;
+                                       base.HeightRequest = 2 * value;
+                                       base.BorderRadius = value;
+                               }
+                       }
+
+                       public new double WidthRequest
+                       {
+                               get
+                               {
+                                       return base.WidthRequest;
+                               }
+
+                               set
+                               {
+                                       base.WidthRequest = value;
+                                       base.HeightRequest = value;
+                                       base.BorderRadius = ((int)value) / 2;
+                               }
+                       }
+
+                       public new double HeightRequest
+                       {
+                               get
+                               {
+                                       return base.HeightRequest;
+                               }
+
+                               set
+                               {
+                                       base.WidthRequest = value;
+                                       base.HeightRequest = value;
+                                       base.BorderRadius = ((int)value) / 2;
+                               }
+                       }
+
+               }
+               protected override void Init()
+               {
+                       Content = new RoundedButton(100);
+               }
+       }
+}
index 81a6c2e..bca0e29 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla39499.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla39668.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla39829.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla39853.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)_Template.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue1028.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue1075.cs" />
index 60ee641..131e5f9 100644 (file)
@@ -88,6 +88,14 @@ namespace Xamarin.Forms.Platform.WinRT
                        }
                }
 
+               protected override void UpdateBackgroundColor()
+               {
+                       // Button is a special case; we don't want to set the Control's background
+                       // because it goes outside the bounds of the Border/ContentPresenter, 
+                       // which is where we might change the BorderRadius to create a rounded shape.
+                       return;
+               }
+
                void OnButtonClick(object sender, RoutedEventArgs e)
                {
                        Button buttonView = Element;
@@ -97,7 +105,7 @@ namespace Xamarin.Forms.Platform.WinRT
 
                void UpdateBackground()
                {
-                       Control.Background = Element.BackgroundColor != Color.Default ? Element.BackgroundColor.ToBrush() : (Brush)Windows.UI.Xaml.Application.Current.Resources["ButtonBackgroundThemeBrush"];
+                       Control.BackgroundColor = Element.BackgroundColor != Color.Default ? Element.BackgroundColor.ToBrush() : (Brush)Windows.UI.Xaml.Application.Current.Resources["ButtonBackgroundThemeBrush"];
                }
 
                void UpdateBorderColor()
index 73c2f92..ffe75d4 100644 (file)
@@ -1,7 +1,9 @@
 using Windows.UI.Xaml;
 using Windows.UI.Xaml.Controls;
+using Windows.UI.Xaml.Media;
 
 #if WINDOWS_UWP
+using WContentPresenter = Windows.UI.Xaml.Controls.ContentPresenter;
 
 namespace Xamarin.Forms.Platform.UWP
 #else
@@ -11,36 +13,87 @@ namespace Xamarin.Forms.Platform.WinRT
 {
        public class FormsButton : Windows.UI.Xaml.Controls.Button
        {
-               public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register("BorderRadius", typeof(int), typeof(FormsButton),
+               public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register(nameof(BorderRadius), typeof(int), typeof(FormsButton),
                        new PropertyMetadata(default(int), OnBorderRadiusChanged));
 
+               public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register(nameof(BackgroundColor), typeof(Brush), typeof(FormsButton),
+                       new PropertyMetadata(default(Brush), OnBackgroundColorChanged));
+
+#if WINDOWS_UWP
+               WContentPresenter _contentPresenter;
+#else
                Border _border;
+#endif
+
+               public Brush BackgroundColor
+               {
+                       get
+                       {
+                               return (Brush)GetValue(BackgroundColorProperty);
+                       }
+                       set
+                       {
+                               SetValue(BackgroundColorProperty, value);
+                       }
+               }
 
                public int BorderRadius
                {
-                       get { return (int)GetValue(BorderRadiusProperty); }
-                       set { SetValue(BorderRadiusProperty, value); }
+                       get
+                       {
+                               return (int)GetValue(BorderRadiusProperty);
+                       }
+                       set
+                       {
+                               SetValue(BorderRadiusProperty, value);
+                       }
                }
 
                protected override void OnApplyTemplate()
                {
                        base.OnApplyTemplate();
 
+#if WINDOWS_UWP
+                       _contentPresenter = GetTemplateChild("ContentPresenter") as WContentPresenter;  
+#else
                        _border = GetTemplateChild("Border") as Border;
+#endif
+                       UpdateBackgroundColor();
                        UpdateBorderRadius();
                }
 
+               static void OnBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+               {
+                       ((FormsButton)d).UpdateBackgroundColor();
+               }
+
                static void OnBorderRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
                {
                        ((FormsButton)d).UpdateBorderRadius();
                }
 
+               void UpdateBackgroundColor()
+               {
+                       Background = Color.Transparent.ToBrush();
+#if WINDOWS_UWP
+                       if (_contentPresenter != null)
+                               _contentPresenter.Background = BackgroundColor;
+#else
+                       if (_border != null)
+                               _border.Background = BackgroundColor;
+#endif
+               }
+
                void UpdateBorderRadius()
                {
-                       if (_border == null)
-                               return;
 
-                       _border.CornerRadius = new CornerRadius(BorderRadius);
+#if WINDOWS_UWP
+                       if (_contentPresenter != null)
+                               _contentPresenter.CornerRadius = new CornerRadius(BorderRadius);
+#else
+                       if (_border != null)
+                               _border.CornerRadius = new CornerRadius(BorderRadius);
+#endif
                }
        }
 }
\ No newline at end of file