【问题标题】:custom BindableProperty of HeightRequest for my custom control in xamarin.forms我在 xamarin.forms 中的自定义控件的 HeightRequest 的自定义 BindableProperty
【发布时间】:2018-03-03 13:12:01
【问题描述】:

我为 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

我在这里做错了什么。请帮忙。

提前谢谢你。

【问题讨论】:

    标签: properties xamarin.forms bindable


    【解决方案1】:

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

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

    1. propertyName: "HeightRequest" 应该是propertyName: "HeightText"

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

    3. 并使用GetValueSetValue 添加HeightText 属性

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

    【讨论】:

    • 我没有明确地转换我的默认值,所以它正在评估为一个 int。谢谢
    【解决方案2】:

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

    代码

    public static readonly BindableProperty HeightRequestProperty =
        BindableProperty.Create<InstallationPhotoWrappanel,double>(i=>i.HeightRequest,100,BindingMode.TwoWay,heightTextPropertyChanged);
    
    public double HeightRequest
    {
        get
        {
            return (double)GetValue(HeightRequestProperty);
        }
        set
        {
            SetValue(HeightRequestProperty, value);
        }
    }
    
    static bool heightTextPropertyChanged(BindableObject bindable, double value)
    {
        var control = (InstallationPhotoWrappanel)bindable;
        control.HeightRequest = value;
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 2020-01-30
      • 2018-02-17
      相关资源
      最近更新 更多