Why the slider gets a value different from the one I set?

uselessChiPuselessChiP USMember
edited January 2016 in Xamarin.Forms

So, this is part of my c# code:

        private float StepValue = 5.0f;

        public SettingsPage()
        {
            InitializeComponent();

            DurationSlider.ValueChanged += OnValueChanged;
        }

        private void OnValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == e.OldValue)
                return;

            var h = DurationSlider.Value;
            float newStep = (float)Math.Round(e.NewValue / StepValue);
            float newValue = newStep * StepValue;

            DurationSlider.Value = newValue;
        }

With its xaml code:

<Label BindingContext="{x:Reference DurationSlider}" Text="{Binding Value}" x:Name="Test" />
<Slider HorizontalOptions="FillAndExpand" Maximum="3540" Minimum="0" Value="1502" x:Name="DurationSlider"/>

When I use the debugger to see the value got by the slider, it's always something not integer and near the number I want but it's not the correct value.
If instead of newValue I write directly the numeric value in "DurationSlider.Value", for example 1500, i get the right result.

I'm fairly new to xamarin and i could be missing something but I don't see the problem. The only thing that comes to my mind is that xamarin is doing something behind the scene that causes a rounding error but I find it improbable, seeing that newValue is surely a whole number.

Am I doing something wrong?

Thanks.

Sign In or Register to comment.