0

我有一个自定义控件,我想定义一个FontSizeProperty,我的访问者可以使用它:

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

我尝试了一些东西,但应用程序总是崩溃。

尝试1:

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

例外:

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

尝试2:

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

例外:

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

尝试3:

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

例外:

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

我应该使用什么作为我的默认值FontSizeProperty

4

2 回答 2

1

我认为您缺少的只是字体大小的默认数字,例如 12d。您可能需要将 d 显式添加到数字中,否则它可能会在运行时认为它是 int 而不是 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); }
}
于 2017-07-28T14:08:13.180 回答
0

也许尝试 12 作为默认值或您想要的任何数字。实例化控件时,您始终可以将其设置为其他值。

于 2017-07-28T13:55:28.120 回答