Xamarin.iOS中的自定义控件/ UIView,具有设计时属性

时间:2016-08-08 09:49:53

标签: c# iphone xamarin uiview xamarin.ios

我正在为iOS应用创建用户界面,我正在寻找创建可重用自定义控件的正确方法。我在运行应用程序时通常会使用它,但在设计时设置我的“导出”属性在设计器中没有明显的效果。我认为我做的事情从根本上是错误的,所以也许有人可以给我指导

我在做什么:

  • 我创建了UIControl
  • 的子类
  • 在构造函数中,我调用Initialize方法。
  • Initialize方法中,我添加了几个子视图和约束来在我的控件中布局它们

以下是一些挖空的代码,显示了上述内容:

  [Register("RangedValueSelector"), DesignTimeVisible(true)]
public sealed class RangedValueSelector : UIControl
{  
    public RangedValueSelector(IntPtr p)
            : base(p)
    {
        Initialize();
    }

    public RangedValueSelector()
    {
        Initialize();
    }

    public int HorizontalButtonSpacing
    {
        get { return _horizontalButtonSpacing; }
        set
        {
            _horizontalButtonSpacing = value;
        }
    }


    [Export("LabelBoxVerticalInset"), Browsable(true)]
    public int LabelBoxVerticalInset
    {
        get
        {
            return _labelBoxVerticalInset;
        }

        set
        {
            _labelBoxVerticalInset = value;
        }
    }

      private void Initialize()
    {
        //Code that creates and add Subviews
        //Code that creates and add the required constraints, some of which should depend on the design time properties
    }
}

因此,如果我通过设计器设置导出的属性,则控件可以正常工作,但是它们对设计器本身没有立竿见影的效果。

使用设计时可设置属性更改约束值的建议方法是什么?我希望每次代码中的某个人或设计人员设置属性时都不必重新创建所有子视图。

1 个答案:

答案 0 :(得分:0)

您缺少设计者使用的RectangleF构造函数。

  

public RangedValueSelector(RectangleF bounds):base(bounds){}

其余的似乎是正确的。