【问题标题】:Xamarin.iOS: Create a custom button with an additional objectXamarin.iOS:使用附加对象创建自定义按钮
【发布时间】:2016-04-01 20:25:36
【问题描述】:

在我的 Xamarin.iOS 应用程序中,有许多按钮具有相同的方案,但与标准 UIButton 不同。我为按钮创建了一个类,因为大多数功能是相同的,但例如 textcolor 或 backgroundcolor 是不同的。

那么我怎样才能在情节提要中添加有关任何按钮的额外信息? 我如何在代码中对此做出反应?

【问题讨论】:

    标签: ios xamarin xamarin.ios storyboard


    【解决方案1】:

    您可以使用 DesignTimeVisibleRegister 属性使您的自定义元素对设计器可见

    [Register ("CustomButton"), DesignTimeVisible (true)]
    public class CustomButton: UIButton {
    
        [Export ("CustomProperty "), Browsable (true)]
        public int CustomProperty {get; set;}
    
        public CustomButton(IntPtr handle) : base (handle) { }
    
        public CustomButton()
        {
            // Called when created from code.
            Initialize ();
        }
    
        public override void AwakeFromNib ()
        {
            // Called when loaded from xib or storyboard.
            Initialize ();
        }
    
        void Initialize ()
        {
            // Common initialization code here.
            CustomProperty = 0xB00B5;
        }
    }
    

    对于您想在设计器中设置的所有属性,您只需添加ExportBrowsable (true)。在Initialize你可以设置所有常用属性的值。

    它将出现在Custom Components 下的工具箱中。你可能不得不重建。

    并且可以在Properties 窗格中修改自定义属性

    更多信息:https://developer.xamarin.com/guides/ios/user_interface/designer/ios_designable_controls_overview/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-24
      • 2016-12-27
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多