【问题标题】:How to stop Visual Studio 2017 from updating designer files?如何阻止 Visual Studio 2017 更新设计器文件?
【发布时间】:2017-07-19 10:32:49
【问题描述】:

我需要重新设计旧的 Windows 窗体应用程序。所以我决定创建一个自定义按钮控件。

假设我为该自定义按钮定义了一个背景颜色为红色。当我这样做时:

  • 将该按钮放在表单上
  • 编辑任何其他控件的属性
  • 保存项目

Visual Studio 会自动更新设计器文件并追加:

this.btnCustom.BackColor = System.Drawing.Color.Red;

我不希望这种情况发生。这应该只在 CustomButton 类中编码,而不是在设计器文件中。如果我决定将按钮颜色更改为蓝色,这将是一个大问题。然后,仅在 CustomButton 类中更改颜色,我将不得不为每个按钮手动更改颜色。有什么办法可以预防吗?

【问题讨论】:

标签: winforms visual-studio visual-studio-2017 windows-forms-designer


【解决方案1】:

如果您的 CustomControl 代码是这样的:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override Color BackColor
    {
        get
        {
            return Color.Red;
        }
    }
}

然后,当放置在 Form 上时,设计器不会为 BackgroundColor 生成 setter:

        // 
        // userControl11
        // 
        this.userControl11.Location = new System.Drawing.Point(67, 131);
        this.userControl11.Name = "userControl11";
        this.userControl11.Size = new System.Drawing.Size(154, 37);
        this.userControl11.TabIndex = 2;

因此,如果您稍后更改您的 CustomControl,它会随处更改。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
相关资源
最近更新 更多