I am new to this awesome platform, I made very simple example it contains only one XAML page. Yesterday it worked flawlessly, but when ran it today, it threw this exception out of nowhere
This the exception:
HelloPage.xaml :
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SAP.HelloPage">
<!-- la configuration personalisé pour chaque system d'éxploitation -->
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness"
iOS="0,10,0,0"
Android="0,40,0,0">
</OnPlatform>
</ContentPage.Padding>
<StackLayout HorizontalOptions="Center" BindingContext="{x:Reference sliderID}">
<Button Clicked="Button_Clicked" Text="Suivant"/>
<Label x:Name="lb_font" Text="Font is :"/>
<Slider Minimum="16" Maximum="45" x:Name="sliderID" ValueChanged="sliderID_ValueChanged"/>
<Label x:Name="lb_quote"/>
</StackLayout>
<ContentPage>
HelloPage.xaml.cs :
public partial class HelloPage : ContentPage
{
// quotes
List<string> quotes = new List<string>()
{
"Bienvenu, la première paragraphe",
"une autre quote, cella j'adore",
"La troisième est magnifique"
};
int pos = 0;
public HelloPage()
{
InitializeComponent();
// settings initialisation
lb_quote.Text = quotes.ElementAt(0);
}
private void Button_Clicked(object sender, EventArgs e)
{
if(pos == quotes.Count)
{
pos = 0;
}
lb_quote.Text = quotes.ElementAt(pos);
pos += 1;
}
private void sliderID_ValueChanged(object sender, ValueChangedEventArgs e)
{
lb_font.Text ="Font Size : " + sliderID.Value.ToString();
lb_quote.FontSize = sliderID.Value;
}
}
This one is quite subtle and hard to catch (well, actually it isn't hard to catch; just put a try/catch
around InitializeComponent
and you can examine the exception).
XAML is declarative which makes us believe that the order of attributes of a control does not matter. Unfortunately, since at some point the declarative XAML will be turned into a sequence of property assignments, the order of attributes does matter and it's the order of your Slider
attributes that is causing your exception to be thrown.
You can imagine your Slider
being constructed the following way:
var slider = new Slider();
slider.Minimum = 16;
slider.Maximum = 45;
...
But Slider.Minimum
and Slider.Maximum
seem to check if the values passed are valid.
When your code begins, the value of Maximum
defaults to 0
. But before its value can be assigned, Minimum
's value is assigned a value of 16
.
At this moment your Minimum
value (16) is greater than the default Maximum
value (0) and hence an ArgumentOutOfRangeException
is thrown.
To solve it, just set the Maximum
before the Minimum
value and it does work.
As @Paul mentioned. Wrap it with try-catch and it gives more details about the exception. I spent few hours before figuring out to do that
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With