Search code examples
c#xamarinxamarin.formsproperty-binding

Create FontSizeProperty


I have a custom control and I want to define a FontSizeProperty, which my accessor can use:

public double FontSize
{
    get
    {
        return (double)GetValue(FontSizeProperty);
    }
    set
    {
        SetValue(FontSizeProperty, value);
        this.TextContainer.FontSize = value;
    }
}

I've tried a few things, but the app always crashes.

Try 1:

public static readonly BindableProperty FontSizeProperty =
        BindableProperty.Create("FontSize", 
                                typeof(double), 
                                typeof(MultiLineButton), 
                                (double)Label.FontSizeProperty.DefaultValue);

Exception:

Unhandled Exception:
System.FormatException: Input string was not in a correct format.
  at System.Number.ParseDouble (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) [0x00087] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/number.cs:728 
  at System.Double.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/double.cs:262 
  at System.Double.Parse (System.String s, System.IFormatProvider provider) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/double.cs:245 
  at Xamarin.Forms.Xaml.TypeConversionExtensions.ConvertTo (System.Object value, System.Type toType, System.Func`
1[TResult] getConverter, System.IServiceProvider serviceProvider) [0x001f5] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\TypeConversionExtensions.cs:152 
  at Xamarin.Forms.Xaml.TypeConversionExtensions.ConvertTo (System.Object value, System.Type toType, System.Func`1[TResult] minfoRetriever, System.IServiceProvider serviceProvider) [0x00021] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\TypeConversionExtensions.cs:74 
  at Xamarin.Forms.Xaml.ValueConverterProvider.Convert (System.Object value, System.Type toType, System.Func`1[TResult] minfoRetriever, System.IServiceProvider serviceProvider) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ValueConverterProvider.cs:15 
  at Xamarin.Forms.Setter.Xamarin.Forms.Xaml.IValueProvider.ProvideValue (System.IServiceProvider serviceProvider) [0x00062] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Setter.cs:34 
  at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.F
orms.Xaml.INode parentNode) [0x0004d] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:96 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x000b1] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:175 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00089] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:172 
  at Xamarin.Forms.Xaml.FillResourceDictionariesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x0017e] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\FillResourceDictionariesVisitor.cs:97 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:165 
  at Xamarin.For
ms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00089] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:172 
  at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00054] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:223 
  at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext) [0x0006d] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:124 
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x00046] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:76 
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:56 
  at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (
TXaml view, System.Type callingType) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ViewExtensions.cs:36 
  at SomeProject.App.InitializeComponent () [0x00001] in C:\Users\some-user\project\obj\Debug\SomeProject.App.xaml.g.cs:19 
  at SomeProject.App..ctor () [0x00019] in C:\Users\some-user\project\App.xaml.cs:68 
  at SomeProject.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication app, Foundation.NSDictionary options) [0x000bc] in C:\Users\some-userproject.iOS\AppDelegate.cs:38 
  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/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:79 
  at UIKit.UIApplication.Main (System.String[] args, System.String prin
cipalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at SomeProject.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\some-userproject.iOS\Main.cs:17 
2017-07-28 15:04:29.748 SomeProjectiOS[8511:191052] Unhandled managed exception:
Input string was not in a correct format. (System.FormatException)
  at System.Number.ParseDouble (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) [0x00087] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/number.cs:728 
  at System.Double.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/double.cs:262 
  at S
ystem.Double.Parse (System.String s, System.IFormatProvider provider) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/referencesource/mscorlib/system/double.cs:245 
  at Xamarin.Forms.Xaml.TypeConversionExtensions.ConvertTo (System.Object value, System.Type toType, System.Func`1[TResult] getConverter, System.IServiceProvider serviceProvider) [0x001f5] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\TypeConversionExtensions.cs:152 
  at Xamarin.Forms.Xaml.TypeConversionExtensions.ConvertTo (System.Object value, System.Type toType, System.Func`1[TResult] minfoRetriever, System.IServiceProvider serviceProvider) [0x00021] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\TypeConversionExtensions.cs:74 
  at Xamarin.Forms.Xaml.ValueConverterProvider.Convert (System.Object value, System.Type toType, System.Func`1[TResult] minfoRetriever, System.IServiceProvider serviceProvider) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\Valu
eConverterProvider.cs:15 
  at Xamarin.Forms.Setter.Xamarin.Forms.Xaml.IValueProvider.ProvideValue (System.IServiceProvider serviceProvider) [0x00062] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\Setter.cs:34 
  at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x0004d] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:96 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x000b1] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:175 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00089] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:172 
  at Xamarin.Forms.Xaml.FillResourceDictionariesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x0017e] in
 C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\FillResourceDictionariesVisitor.cs:97 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:165 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00089] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:172 
  at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00054] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlNode.cs:223 
  at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext) [0x0006d] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:124 
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Strin
g xaml) [0x00046] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:76 
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\XamlLoader.cs:56 
  at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Xaml\ViewExtensions.cs:36 
  at SomeProject.App.InitializeComponent () [0x00001] in C:\Users\some-user\project\obj\Debug\SomeProject.App.xaml.g.cs:19 
  at SomeProject.App..ctor () [0x00019] in C:\Users\some-user\project\App.xaml.cs:68 
  at SomeProject.iOS.AppDelegate.FinishedLaunching (UIKit.UIApplication app, Foundation.NSDictionary options) [0x000bc] in C:\Users\some-userproject.iOS\AppDelegate.cs:38 
  at (wrapper managed-to-native) UIKit.UIApplication:UI
ApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/4691/d2270eec/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/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:63 
  at SomeProject.iOS.Application.Main (System.String[] args) [0x00001] in C:\Users\some-userproject.iOS\Main.cs:17

Try 2:

public static readonly BindableProperty FontSizeProperty =
        BindableProperty.Create("FontSize", 
                                typeof(double), 
                                typeof(MultiLineButton), 
                                Device.GetNamedSize(NamedSize.Medium, typeof(Label)));

Exception:

Unhandled Exception:
System.FormatException: Input string was not in a correct format.

Try 3:

public static readonly BindableProperty FontSizeProperty =
        BindableProperty.Create("FontSize", 
                                typeof(double), 
                                typeof(MultiLineButton), 
                                null);

Exception:

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'SomeProject.Views.MultiLineButton' threw an exception. ---> System.ArgumentException: Not a valid default value
Parameter name: defaultValue

What should I use as default value for my FontSizeProperty?


Solution

  • I think all your missing is the default number for your font size for example 12d. You'll probably need to explicitly add the d to the number or it may think it is an int at runtime instead of a double.

    public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(double), typeof(MultiLineButton), 12d);
    
    [TypeConverter(typeof(FontSizeConverter))]
    public double FontSize
    {
        get { return (double)GetValue(FontSizeProperty); }
        set { SetValue(FontSizeProperty, value); }
    }