Bug 47805 - Exception when binding Slider Minimum, Maximum and Value properties.
Summary: Exception when binding Slider Minimum, Maximum and Value properties.
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-11-24 00:04 UTC by Jon Goldberger [MSFT]
Modified: 2017-09-20 08:41 UTC (History)
7 users (show)

Tags: ac
Is this bug a regression?: Yes
Last known good build: None for iOS, 2.3.2.127 for Android


Attachments
Test Project (268.47 KB, application/zip)
2016-11-24 00:04 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 47805 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Jon Goldberger [MSFT] 2016-11-24 00:04:13 UTC
Created attachment 18630 [details]
Test Project

## Description

Exception thrown when setting the Minimum, Maximum, and Value properties of a slider via binding to a view model.

>System.ArgumentException: Value was an invalid value for Minimum
>Parameter name: value
>  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
>  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:79
>  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:63
>  at TestCase4.iOS.Application.Main (System.String[] args) [0x00008] in /Users/jongoldberger/Downloads/TestCase4-ModifiedAgain/TestCase4/TestCase4.iOS/Main.cs:17

The issue seems to be that the Minimum value for the slider is set before the old Maximum value is changed, and the old maximum value is lower than the new minimum value. Note that I am explicitly setting the bound property for MaxNeutral (bound to the Maximum slider property) to be higher than the new MinNeutral (bound to the Minimum slider property) before setting MinNeutral. Code:

>public ServoType ServoType
>        {
>            get
>            {
>				return _ServoType;
>            }
>            set
>            {
>				
>				if (value != _ServoType)
>					_ServoType = value;
>
>				// Make sure the min and max are outside any possible value of Neutral
>				MinNeutral = 0;
>				MaxNeutral = 2000;
>
>				switch (_ServoType)
>				{
>					case ServoType.NoServo:
>						Neutral = 0;
>						MinNeutral = 0;
>						MaxNeutral = 1;
>						break;
>					case ServoType.Standard:
>						Neutral = 1520;
>						// Neutral is bound to the Value property of the slider as a TwoWay binding.
>						// MaxNeutral is bound to the Maximum property of the slider.
>						// MinNeutral is bound to the Minimun property of the slider. 
>						// Exception occurs when going from Gyro to Standard as the max for Gyro is less than the min for Standard,
>						// though it seems it should also throw when going from NoServo to Standard 
>						// as again the Gyro MaxNeutral value is lower than the new MinNeutral value. 
>						// If I change the value of MinNeutral to 998 or lower below, then exception is not thrown
>						// So it seems that the max value for the slider is not changed yet from the Gyro setting for
>						// MaxNeutral so then setting MinNeutral to be > MaxNeutral (the min and max vlues allowed for the slider)
>						// throws the error despite setting MaxNeutral above to be higher than the new MinNeutral.
>						// If I set the slider's Minimum and Maximum values explicitly to 0 and 2000 respectively in the view class,
>						// test.xaml.cs, that also resolves the particula issue, but causes some other issues, like the Neutral property
>						// not being bound correctly. 
>						MinNeutral = 1000;
>						MaxNeutral = 2000;
>						break;
>					case ServoType.Gyro:
>						Neutral = 760;
>						MinNeutral = MinUpTime + MinDownTime + 1;
>						MaxNeutral = 999;
>						break;
>				}
>            }
>        }

## Steps to reproduce

1. Open the attached test project.

2. Deploy iOS (or Android) app project to simulator/device.

3. Click "Gyro" button

4. Click "Standard" button

Expected result: Slider will be updated with new Minimum, Maximum and Value and the label above the slider will display "1520.0"

Actual result: Exception above is thrown. 

## Notes

At first I saw this only as an iOS issue. The original test project sent from the reporting customer was using XF version 2.3.2.127. After testing to see if perhaps the issue was resolved in the latest stable, 2.3.3.168, I noted the issue then also occurred on Android. 

## Regression status:

Since I noticed the regression in Android after updating to XF 2.3.3.168, I tried to see if I could find a version that worked for iOS, to no avail yet. 

XF 2.3.3.168
Android: Bad
iOS : Bad

XF 2.3.2.127
Android: Good
iOS : Bad

XF 2.3.1.114
Android: Good
iOS : Bad

XF 2.3.0.107
Android: Good
iOS : Bad

XF 2.0.0.6482
Android: Good
iOS : Bad

XF 1.5.1.6471
Android: Good
iOS : Bad

## Possible workaround

Set the Minimum and Maximum properties for the slider to 0 and 2000 respectively directly in the view class, test.xaml.cs, in the button click event handlers to guarantee that the Minimum and Maximum values are set to values that will not conflict with the correct MinNeutral and MaxNeutral values that are then set in the view model when the new ServoType is set. 


## Environment

=== Xamarin Studio Enterprise ===

Version 6.1.2 (build 44)
Installation UUID: ceaba76c-db06-4fbd-b326-f69ea53c3e01
Runtime:
	Mono 4.6.2 (mono-4.6.0-branch/08fd525) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406020007

=== NuGet ===

Version: 3.4.3.0

=== Xamarin.Profiler ===

Version: 0.33.1
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 7.0.2.37 (Visual Studio Enterprise)
Android SDK: /Users/jongoldberger/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)
		7.0   (API level 24)

SDK Tools Version: 25.2.2
SDK Platform Tools Version: 24.0.3
SDK Build Tools Version: 24.0.3

Java SDK: /usr
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 8.1 (11544)
Build 8B62

=== Xamarin.Mac ===

Version: 2.10.0.113 (Visual Studio Enterprise)

=== Xamarin.iOS ===

Version: 10.2.1.5 (Visual Studio Enterprise)
Hash: 44931ae
Branch: xcode8.1
Build date: 2016-11-01 20:52:28-0400

=== Xamarin Inspector ===

Version: 0.10.0.0
Hash: e931a52
Branch: master
Build date: Thu, 18 Aug 2016 17:46:46 GMT

=== Build Information ===

Release ID: 601020044
Git revision: 0ccfcd52b95305ebd5b7eca0d88c1017035910ae
Build date: 2016-10-28 15:12:43-04
Xamarin addins: a39a869d8a78d87bdc6775f696c13a4cc9024501
Build lane: monodevelop-lion-cycle8

=== Operating System ===

Mac OS X 10.12.1
Darwin Jons-MacBook-Pro.local 16.1.0 Darwin Kernel Version 16.1.0
    Thu Oct 13 21:26:57 PDT 2016
    root:xnu-3789.21.3~60/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

Xamarin Inspector 0.10.0.0
Comment 1 adrianknight89 2016-11-24 07:09:02 UTC
This seems to be introduced in 2.3.3-pre3. I suspect that it has to do with Stephane's XAML changes, but that's just a guess.
Comment 2 Jon Goldberger [MSFT] 2016-11-28 18:01:49 UTC
Note that this issue does occur on iOS as far back as XF 1.5.1.6471 (did not test older versions than that).
Comment 4 Mick George 2017-06-14 12:00:38 UTC
Is there any ETA on a fix or reliable workaround? I am using Xamarin Forms version 2.3.4.247 and I am seeing this defect with my Android project and I can not get any work around to work reliably.
Comment 5 Evgheni Obrucicov 2017-09-20 08:23:23 UTC
Until this bug is fixed i use the following workaround:

xaml:  

<Slider Maximum="1" Minimum="0" Value="{Binding RollSliderValue, Mode=TwoWay}" 

code:

    public class ModRoll
    {
        public int Min { get; set; }
        public int Max { get; set; }
        public int Roll { get; set; }
        public double RollSliderValue {
            get
            {
                return  (double)(Roll - Min)/ (double)(Max - Min);
            }
            set
            {
                Roll = Min + (int)((Max - Min) * value);
            }
        }
        
    }

Then just change BindigSource of a slider to a new ModRoll object when needed.
Comment 6 Evgheni Obrucicov 2017-09-20 08:41:52 UTC
Opps, i ment
Roll = Min + (int)Math.Round((Max - Min) * value);