This commit is contained in:
Ravinder Jangra 2018-02-06 13:07:36 +05:30
Родитель f371dd59a4
Коммит 9f9dc82c95
3 изменённых файлов: 125 добавлений и 17 удалений

Просмотреть файл

@ -13,6 +13,11 @@ namespace Xamarin.Forms.Platform.Android
{
double _max, _min;
bool _progressChangedOnce;
ColorStateList defaultprogresstintlist, defaultprogressbackgroundtintlist;
ColorFilter defaultthumbcolorfilter;
Drawable defaultthumb;
PorterDuff.Mode defaultprogresstintmode, defaultprogressbackgroundtintmode;
public SliderRenderer(Context context) : base(context)
{
@ -65,8 +70,18 @@ namespace Xamarin.Forms.Platform.Android
SetNativeControl(seekBar);
seekBar.Max = 1000;
seekBar.SetOnSeekBarChangeListener(this);
//defaultthumbcolorfilter = Control.Thumb.ColorFilter;
//defaultprogresstintlist = Control.ProgressTintList;
//defaultprogressbackgroundtintlist = Control.ProgressBackgroundTintList;
defaultthumbcolorfilter = seekBar.Thumb.ColorFilter;
defaultprogresstintmode = seekBar.ProgressTintMode;
defaultprogressbackgroundtintmode = seekBar.ProgressBackgroundTintMode;
defaultprogresstintlist = seekBar.ProgressTintList;
defaultprogressbackgroundtintlist = seekBar.ProgressBackgroundTintList;
defaultthumb = seekBar.Thumb;
}
Slider slider = e.NewElement;
@ -126,35 +141,62 @@ namespace Xamarin.Forms.Platform.Android
private void UpdateMinimumTrackColor()
{
if (Element != null && Element.MinimumTrackColor != Color.Default)
if (Element != null)
{
Control.ProgressDrawable.SetColorFilter(new PorterDuffColorFilter(Element.MinimumTrackColor.ToAndroid(), PorterDuff.Mode.SrcIn));
if (Element.MinimumTrackColor == Color.Default)
{
Control.ProgressTintList = defaultprogresstintlist;
Control.ProgressTintMode = defaultprogresstintmode;
}
else
{
Control.ProgressTintList = ColorStateList.ValueOf(Element.MinimumTrackColor.ToAndroid());
Control.ProgressTintMode = PorterDuff.Mode.SrcIn;
}
}
}
private void UpdateMaximumTrackColor()
{
if (Element != null && Element.MaximumTrackColor != Color.Default)
if (Element != null)
{
Control.ForegroundTintList = ColorStateList.ValueOf(Element.MinimumTrackColor.ToAndroid());
Control.ProgressBackgroundTintList = ColorStateList.ValueOf(Element.MaximumTrackColor.ToAndroid());
Control.ProgressBackgroundTintMode = PorterDuff.Mode.SrcIn;
if (Element.MaximumTrackColor == Color.Default)
{
Control.ProgressBackgroundTintList = defaultprogressbackgroundtintlist;
Control.ProgressBackgroundTintMode = defaultprogressbackgroundtintmode;
}
else
{
Control.ProgressBackgroundTintList = ColorStateList.ValueOf(Element.MaximumTrackColor.ToAndroid());
Control.ProgressBackgroundTintMode = PorterDuff.Mode.SrcIn;
}
}
}
private void UpdateThumbColor()
{
if (Element != null && Element.ThumbColor != Color.Default)
if (Element != null)
{
Control.Thumb.SetColorFilter(Element.ThumbColor.ToAndroid(), PorterDuff.Mode.SrcIn);
if (Element.ThumbColor == Color.Default)
{
Control.Thumb.SetColorFilter(defaultthumbcolorfilter);
}
else
{
Control.Thumb.SetColorFilter(Element.ThumbColor.ToAndroid(), PorterDuff.Mode.SrcIn);
}
}
}
private void UpdateThumbImage()
{
if (Element != null && !string.IsNullOrEmpty(Element.ThumbImage))
if (Element != null)
{
Control.SetThumb(Context.GetDrawable(Element.ThumbImage));
if (string.IsNullOrEmpty(Element.ThumbImage))
Control.SetThumb(defaultthumb);
else
Control.SetThumb(Context.GetDrawable(Element.ThumbImage));
}
}

Просмотреть файл

@ -2,11 +2,15 @@
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
namespace Xamarin.Forms.Platform.UWP
{
public class SliderRenderer : ViewRenderer<Slider, Windows.UI.Xaml.Controls.Slider>
{
Brush defaultforegroundcolor;
Brush defaultbackgroundcolor;
protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
{
base.OnElementChanged(e);
@ -24,6 +28,10 @@ namespace Xamarin.Forms.Platform.UWP
slider.ValueChanged += OnNativeValueChanged;
defaultforegroundcolor = slider.Foreground;
defaultbackgroundcolor = slider.Background;
// 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
@ -45,6 +53,37 @@ namespace Xamarin.Forms.Platform.UWP
Control.StepFrequency = stepping;
Control.SmallChange = stepping;
UpdateFlowDirection();
UpdateSliderColors();
}
}
private void UpdateSliderColors()
{
UpdateMinimumTrackColor();
UpdateMaximumTrackColor();
}
private void UpdateMinimumTrackColor()
{
if (Control != null)
{
if (Element.MinimumTrackColor == Color.Default)
Control.Foreground = defaultforegroundcolor;
else
Control.Foreground = Element.MinimumTrackColor.ToBrush();
}
}
private void UpdateMaximumTrackColor()
{
if (Control != null)
{
if (Element.MaximumTrackColor == Color.Default)
Control.Background = defaultbackgroundcolor;
else
Control.Background = Element.MaximumTrackColor.ToBrush();
}
}
@ -63,6 +102,11 @@ namespace Xamarin.Forms.Platform.UWP
}
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateFlowDirection();
else if (e.PropertyName == Slider.MinimumTrackColorProperty.PropertyName)
UpdateMinimumTrackColor();
else if (e.PropertyName == Slider.MaximumTrackColorProperty.PropertyName)
UpdateMaximumTrackColor();
}
protected override void UpdateBackgroundColor()

Просмотреть файл

@ -8,6 +8,7 @@ namespace Xamarin.Forms.Platform.iOS
public class SliderRenderer : ViewRenderer<Slider, UISlider>
{
SizeF _fitSize;
UIColor defaultmintrackcolor, defaultmaxtrackcolor, defaultthumbcolor;
public override SizeF SizeThatFits(SizeF size)
{
@ -36,6 +37,10 @@ namespace Xamarin.Forms.Platform.iOS
Control.SizeToFit();
_fitSize = Control.Bounds.Size;
defaultmintrackcolor = Control.MinimumTrackTintColor;
defaultmaxtrackcolor = Control.MaximumTrackTintColor;
defaultthumbcolor = Control.ThumbTintColor;
// except if your not running iOS 7... then it fails...
if (_fitSize.Width <= 0 || _fitSize.Height <= 0)
_fitSize = new SizeF(22, 22); // Per the glorious documentation known as the SDK docs
@ -66,25 +71,34 @@ namespace Xamarin.Forms.Platform.iOS
private void UpdateMinimumTrackColor()
{
if (Element != null && Element.MinimumTrackColor != Color.Default)
if (Element != null)
{
Control.MinimumTrackTintColor = Element.MinimumTrackColor.ToUIColor();
if (Element.MinimumTrackColor == Color.Default)
Control.MinimumTrackTintColor = defaultmintrackcolor;
else
Control.MinimumTrackTintColor = Element.MinimumTrackColor.ToUIColor();
}
}
private void UpdateMaximumTrackColor()
{
if (Element != null && Element.MaximumTrackColor != Color.Default)
if (Element != null)
{
Control.MaximumTrackTintColor = Element.MaximumTrackColor.ToUIColor();
if (Element.MaximumTrackColor == Color.Default)
Control.MaximumTrackTintColor = defaultmaxtrackcolor;
else
Control.MaximumTrackTintColor = Element.MaximumTrackColor.ToUIColor();
}
}
private void UpdateThumbColor()
{
if (Element != null && Element.ThumbColor != Color.Default)
if (Element != null)
{
Control.ThumbTintColor = Element.ThumbColor.ToUIColor();
if (Element.ThumbColor == Color.Default)
Control.ThumbTintColor = defaultthumbcolor;
else
Control.ThumbTintColor = Element.ThumbColor.ToUIColor();
}
}
@ -109,6 +123,14 @@ namespace Xamarin.Forms.Platform.iOS
slider.SetThumbImage(uiimage, UIControlState.Normal);
}
}
else
{
UISlider slider = Control;
if (slider != null)
{
slider.SetThumbImage(null, UIControlState.Normal);
}
}
((IVisualElementController)Element).NativeSizeChanged();
}