简体   繁体   English

C#在运行时停止属性更改

[英]C# stop property change at runtime

I have been trying to build a user control with some custom properties set in the designer. 我一直在尝试使用设计器中设置的一些自定义属性来构建用户控件。 However the control involves some interop code and settings which shouldn't be adjusted at runtime. 但是,控件涉及一些不应在运行时调整的互操作代码和设置。 Is there a way to stop the values being changed after they have been initially set by the designer code? 有没有办法在设计师代码最初设置后停止更改的值?

Are you able to modify the property definition? 你能修改属性定义吗? One approach is, add a sentinel to the property setter, and allow only one set operation (which would usually be done by InitializeComponent()): 一种方法是,向属性setter添加一个sentinel,并且只允许一个set操作(通常由InitializeComponent()完成):

private int _myProperty;
private bool _isMyPropertySet = false;
public int MyProperty
{
    set
    {
        if (!_isMyPropertySet)
        {
            _isMyPropertySet = true;
            _myProperty = value;
        }
        else
        {
            throw new NotSupportedException();
        }
    }
}

Michael provided a great answer, and it will solve your problem at runtime. 迈克尔提供了一个很好的答案,它将在运行时解决您的问题。 However, at design time, if you need to be able to change that value more than once (it is design time, and the probability is likely high), then you will want to combine the DesignMode check with Michaels example: 但是,在设计时,如果您需要能够多次更改该值(这是设计时间,概率可能很高),那么您需要将DesignMode检查与Michaels示例结合使用:

private int _myProperty;
private bool _isMyPropertySet = false;
public int MyProperty
{
    set
    {
        if (this.DesignMode || !_isMyPropertySet)
        {
                _isMyPropertySet = true;
                _myProperty = value;
        }
        else
        {
                throw new NotSupportedException();
        }
    }
}

Now you will be able to edit this value to your hearts content during design, without running into that NotSupportedException() and getting a botched designer on the second set. 现在,您可以在设计过程中将此值编辑到您的内容中,而不会遇到NotSupportedException()并在第二组上获得一个拙劣的设计器。

You could throw an exception inside the property setter? 你可以在属性setter中抛出一个异常?

public int SomeProperty {

   set {

      if(designerComplete) {
          throw new IllegalOperationException();
      }

   }

}

Set designerComplete as a class variable - set it to true after the InitializeComponent method is called in the constructor. 将designerComplete设置为类变量 - 在构造函数中调用InitializeComponent方法后将其设置为true。

The WinForms architecture provides a built-in way to test whether code is currently being executed in design mode - the Component.DesignMode property. WinForms体系结构提供了一种内置方法来测试代码当前是否在设计模式下执行 - Component.DesignMode属性。

So you probably want an implementation something like this: 所以你可能想要一个像这样的实现:

private int _foo;

public int Foo
{
    get { return _foo; }
    set
    {
        if (this.DesignMode)
            throw new InvalidOperationException();

        _foo = value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM