Properties xamarin.forms中自定义控件的HeightRequest的custom BindableProperty

Properties xamarin.forms中自定义控件的HeightRequest的custom BindableProperty,properties,xamarin.forms,bindable,Properties,Xamarin.forms,Bindable,我已经为wrappanel创建了一个自定义控件,但它显示了额外的空间。 因此,我尝试为控件创建HeightRequest的BindableProperty,并根据内容设置它以删除额外的空间 这就是我创建HeightRequest的BindableProperty的方法 public double HeightRequest { get; set; } private static BindableProperty heightTextProperty = BindablePro

我已经为wrappanel创建了一个自定义控件,但它显示了额外的空间。 因此,我尝试为控件创建HeightRequest的BindableProperty,并根据内容设置它以删除额外的空间

这就是我创建HeightRequest的BindableProperty的方法

    public double HeightRequest { get; set; }

    private static BindableProperty heightTextProperty = BindableProperty.Create(
                                                     propertyName: "HeightRequest",
                                                     returnType: typeof(double),
                                                     declaringType: typeof(InstallationPhotoWrappanel),
                                                     defaultValue: 100,
                                                     defaultBindingMode: BindingMode.TwoWay,
                                                     propertyChanged: heightTextPropertyChanged);

    private static void heightTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (InstallationPhotoWrappanel)bindable;
        control.HeightRequest = Convert.ToDouble(newValue);
    }
但这给了我一个例外

exception has been thrown by the target of an invocation
我做错了什么,请帮忙


提前感谢您。

请查看下面的代码并试用。希望对你有帮助

代码
公共静态只读BindableProperty HeightRequestProperty=
创建(i=>i.HeightRequest,100,BindingMode.TwoWay,heightTextPropertyChanged);
公共双高度请求
{
得到
{
返回(双精度)GetValue(HeightRequestProperty);
}
设置
{
设置值(HeightRequestProperty,值);
}
}
静态布尔高度TextPropertyChanged(BindableObject bindable,双值)
{
var控制=(InstallationPhotoWrappanel)可绑定;
control.HeightRequest=值;
返回true;
}

您的自定义控件应该已经有了
HeightRequest
属性。我假设您正在创建一个名为
HeightText
的自定义可绑定属性

如果是这样,我可以在代码中看到三个问题:

  • propertyName:“HeightRequest”
    应该是
    propertyName:“HeightText”

  • 为确保不会出现目标属性类型不匹配异常,请将
    defaultValue:100
    更改为
    defaultValue:(double)100

  • 并使用
    GetValue
    SetValue
    添加
    HeightText
    属性

    public double HeightText
    {
        get
        {
            return (double)GetValue(HeightTextProperty);
        }
        set
        {
            SetValue(HeightTextProperty, value);
        }
    }
    

  • 我没有显式地强制转换我的默认值,所以它的计算结果是int。谢谢
    public double HeightText
    {
        get
        {
            return (double)GetValue(HeightTextProperty);
        }
        set
        {
            SetValue(HeightTextProperty, value);
        }
    }