背景
ContentViewを使って、CustomButtonというカスタムコントロールを作ろうとした
問題
カスタムコントロールに円を含めようとしたので、CustomRadiusという円の半径を表すプロパティを定義した。
その際double型のBindablePropertyをコードビハインドに定義したが、Exceptionエラーが出てアプリが落ちるようになった。
原因
初期値を80という整数にしていたことが原因だった。厳密に80.0など小数点を使って初期値を定義すれば解決した。
public static readonly BindableProperty CustomRadiusProperty =
BindableProperty.Create(nameof(CustomRadius), typeof(double), typeof(CustomButton), 80.0, propertyChanged:OnCustomRadiusChanged);
public double CustomRadius
{
get => (double)GetValue(CustomRadiusProperty);
set => SetValue(CustomRadiusProperty, value);
}
static void OnCustomRadiusChanged (BindableObject bindable, object oldValue, object newValue)
{
~~
}
#endregion CustomRadius Property