Making the Visual Studio designer ignore a public property

核能气质少年 提交于 2019-12-03 11:18:23

Making the property read only at design time will prevent it being serialized into the resx file. Strangely if MyType happens to be a collection the read only is ignored by the designer and you can still set the property at design time even though the property isn't written out into the resx so it's best to make the property not browsable too.

[ReadOnly(true)]
[Browsable(false)]
public MyType MyProperty
{
    get { return _MyProperty; }
    set { _MyProperty = value; }
}

Use [DesignerSerializationVisibilityAttribute ( Visibility = Hidden )]

MSDN Article

Try using a private field with the property's accessor methods along with the [field: NonSerialized] attribute:

[field: NonSerialized]
private MyType _MyProperty;

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public MyType MyProperty
{
    get
    {
        return _MyProperty;
    }
    set
    {
        _MyProperty = value;
    }
}

I failed to find a real solution, but a workaround instead...

I had to go into the Form.resx file and locate the data/value key pair that it was deserializing into my public property. I manually deleted the XML pair contents and then I was able to run the application.

This allowed my application to build and run without errors. Everything else I tried (including deleting the container form for my UserControl and re-creating it repeatedly) did not work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!