Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[WinRT/UWP] Adjust margin for centered Slider #604

Merged
merged 1 commit into from Dec 5, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,49 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

// Apply the default category of "Issues" to all of the tests in this assembly
// We use this as a catch-all for tests which haven't been individually categorized
#if UITEST
[assembly: NUnit.Framework.Category("Issues")]
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Bugzilla, 29110, "[WinRT/UWP] VerticalOptions = LayoutOptions.Center or CenterAndExpand on Sliders does not result in centered display", PlatformAffected.WinRT)]
public class Bugzilla29110 : TestContentPage
{
protected override void Init()
{
Content = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children =
{
new Label
{
BackgroundColor = Color.CadetBlue,
HorizontalOptions = LayoutOptions.Start,
VerticalOptions = LayoutOptions.CenterAndExpand,
VerticalTextAlignment = TextAlignment.Center,
Text = "Label"
},
new Slider
{
BackgroundColor = Color.Green,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
Minimum = 0,
Maximum = 100
}
}
};
}
}
}
Expand Up @@ -39,6 +39,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla29107.xaml.cs">
<DependentUpon>Bugzilla29107.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla29110.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla29158.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla29363.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla29229.cs" />
Expand Down
20 changes: 20 additions & 0 deletions Xamarin.Forms.Platform.WinRT/SliderRenderer.cs
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;

#if WINDOWS_UWP
Expand All @@ -24,6 +25,25 @@ protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
SetNativeControl(slider);

slider.ValueChanged += OnNativeValueCHanged;

// Even when using Center/CenterAndExpand, a Slider has an oddity where it looks
// off-center in its layout by a smidge. The default templates are slightly different
// between 8.1/UWP; the 8.1 rows are 17/Auto/32 and UWP are 18/Auto/18. The value of
// the hardcoded 8.1 rows adds up to 49 (when halved is 24.5) and UWP are 36 (18). Using
// a difference of about 6 pixels to correct this oddity seems to make them both center
// more correctly.
//
// The VerticalAlignment needs to be set as well since a control would not actually be
// centered if a larger HeightRequest is set.
if (Element.VerticalOptions.Alignment == LayoutAlignment.Center && Control.Orientation == Windows.UI.Xaml.Controls.Orientation.Horizontal)
{
Control.VerticalAlignment = VerticalAlignment.Center;
#if WINDOWS_UWP
slider.Margin = new Windows.UI.Xaml.Thickness(0, 7, 0, 0);
#else
slider.Margin = new Windows.UI.Xaml.Thickness(0, 13, 0, 0);
#endif
}
}

double stepping = Math.Min((e.NewElement.Maximum - e.NewElement.Minimum) / 10, 1);
Expand Down