首页 > 解决方案 > Xamarin IOS 自定义控件属性不显示

问题描述

我为我的 Xamarin.IOS 应用程序创建了我自己的自定义控件,并为它创建了一个自定义属性。我想在 Visual Studio 属性工具箱中为它的属性设置值,但是没有 VS 没有 Xcode 不显示任何自定义属性。我必须为自定义按钮设置自定义文本字距间距,并且我不想使用任何棘手或丑陋的方式对其进行排序。

这是我的自定义控制代码:

[Register("CustomButton"), DesignTimeVisible(true)]
    public class CustomButton : UIButton
    {
        private float _textLetterSpacing;

        protected internal CustomButton(IntPtr handle) : base(handle)
        {
        }

        [Export("TextLetterSpacing"), Browsable(true)]
        public float TextLetterSpacing
        {
            get => _textLetterSpacing;
            set
            {
                _textLetterSpacing = value;
                SetNeedsDisplay();
            }
        }

        public CustomButton()
        {
            Initialize();

        }

        public override void AwakeFromNib()
        {
            // Called when loaded from xib or storyboard.
            Initialize();
        }

        void Initialize()
        {

              TextLetterSpacing = -0.49f;
            var title = this.Title(UIControlState.Normal);
            var attributedTitle = new NSAttributedString(title, new UIStringAttributes() { KerningAdjustment = TextLetterSpacing, ForegroundColor = this.CurrentTitleColor });
            this.SetAttributedTitle(attributedTitle, UIControlState.Normal);
            SetNeedsDisplay();
        }

    }

标签: iosxamarinxamarin.ios

解决方案


我想通了这个问题。应该有public构造函数而不是protected internal

public CustomButton(IntPtr handle) : base(handle)
{
}

推荐阅读