Error with Bindable property on usercontrol

FB123FB123 CAMember ✭✭

What am I missing? Whenever I try to set the property value I'm getting the following exception thrown:

Default value did not match return type
Parameter name: defaultValue

Stack Trace...

at Xamarin.Forms.BindableProperty..ctor(String propertyName, Type returnType, Type declaringType, Object defaultValue, BindingMode defaultBindingMode, ValidateValueDelegate validateValue, BindingPropertyChangedDelegate propertyChanged, BindingPropertyChangingDelegate propertyChanging, CoerceValueDelegate coerceValue, BindablePropertyBindingChanging bindingChanging, Boolean isReadOnly, CreateDefaultValueDelegate defaultValueCreator)

My code is the following:

public class ExtendedMenu1 .....

#region ButtonWidth
public const string ButtonWidthPropertyName = "ButtonWidth";
public static readonly BindableProperty ButtonWidthProperty = BindableProperty.Create(ButtonWidthPropertyName, typeof(double), typeof(ExtendedMenu1), double.Parse("10"), BindingMode.OneWay, null, propertyChanged: OnButtonWidthChanged);

private static void OnButtonWidthChanged(BindableObject bindable, object oldvalue, object newvalue)
{
....
}

public double ButtonWidth
{
get { return (double)GetValue(ButtonWidthProperty); }
set { SetValue(ButtonWidthProperty, value); }
}

#endregion

I'm using Xamarin.Forms v2.3.2.127

Answers

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    Default value did not match return type

    you are expecting a double, but parsing a string as a default? why not just set the default to 10.0

  • FB123FB123 CAMember ✭✭

    I tried with the default value originally at 10.0 and got the same exception - so I changed it to a double.parse to confirm I was setting it as a double.

  • XigmondXigmond USMember ✭✭✭
    edited April 2018

    @FB123 said:
    Seems to be working if I change the default value from double.parse("10") or 10.0 to default(double)

    Not really sure that's the best answer since you then can't set the specific default value, you can only set the default value to be the default of the type.

    Hi,
    Change the ButtonWidth Property Definition so that it makes sure to Sets a Double Value :

    public double ButtonWidth
    {
    get { return (double)GetValue(ButtonWidthProperty); }
    set { SetValue(ButtonWidthProperty, (double) value); }
    }

    ps.: I just Noticed how old the post is :smiley: but still maybe it helps someone else

  • kris87kris87 Member ✭✭

    @Xigmond said:

    @FB123 said:
    Seems to be working if I change the default value from double.parse("10") or 10.0 to default(double)

    Not really sure that's the best answer since you then can't set the specific default value, you can only set the default value to be the default of the type.

    Hi,
    Change the ButtonWidth Property Definition so that it makes sure to Sets a Double Value :

    public double ButtonWidth
    {
    get { return (double)GetValue(ButtonWidthProperty); }
    set { SetValue(ButtonWidthProperty, (double) value); }
    }

    ps.: I just Noticed how old the post is :smiley: but still maybe it helps someone else

    it just did, cheers :)

Sign In or Register to comment.