使用界面时,DesignerSerializationVisibility(隐藏),EditorBrowsable(从不)等不起作用

时间:2016-04-30 15:16:58

标签: c# interface visual-studio-2015 user-controls designer

创建界面和用户控件

public interface IMyUserControl
{
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [Bindable(false)]
    [Browsable(false)]
    [ReadOnly(true)]
    object MyProp { get; set; } 
}
public partial class MyUserControl : UserControl, IMyUserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    public object MyProp { get; set; }
}

然后构建项目并将MyUserControl从Toolbox中删除到Form1。 请参见Form1.Designer.cs

private void InitializeComponent()
{
    // other code
    this.myUserControl1.MyProp = null;
    // other code
}

但下面的代码工作正常(设计师不会生成this.myUserControl1.MyProp = null)

public partial class MyUserControl : UserControl, IMyUserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public object MyProp { get; set; }
}

0 个答案:

没有答案