C# 可视化编辑器重命名变量

C# 可视化编辑器重命名变量,c#,.net,winforms,windows-forms-designer,C#,.net,Winforms,Windows Forms Designer,在.Designer文件中,我有一个DataGridViewCellStyle: System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStylePhoto = new System.Windows.Forms.DataGridViewCellStyle(); this.photoDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStylePhoto; 每当我

在.Designer文件中,我有一个
DataGridViewCellStyle

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStylePhoto = new System.Windows.Forms.DataGridViewCellStyle();
this.photoDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStylePhoto;
每当我在VisualEditor中更改与此无关的内容时,它都会坚持这样重命名:

System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
this.photoDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle1;
为什么??我怎样才能防止这种情况

在.Designer文件中,我有一个
DataGridViewCellStyle
。。。每当我在视觉上改变一些与此无关的东西时 编辑,它坚持要重命名

为什么?

每当您在表单设计器中更改任何内容时,
InitializeComponent
方法的整个内容都将重新生成。这就是Windows窗体设计器的工作方式

对于
IComponent
的所有实现,生成代码中变量的名称将与组件的名称相同

我如何防止这种情况发生?


要保留
DataGridViewCellStyle
的变量名没有简单的方法,因为它不是
IComponent
的实现,designer不存储关于其名称的任何信息,并且变量名是动态生成的

他们在总结中告诉我们“设计器支持所需的方法-不要使用代码编辑器修改此方法的内容”。您是否在Designer.cs中编辑某些内容,并在[design]中执行其他操作?您无法阻止它。@Amit:这两种情况都会发生。直接在源代码中编辑或使用[Design]中的可视化编辑器,为什么不改为在普通代码隐藏文件中进行更改?与每次都要重新生成的设计器不同,它永远不会被覆盖。@Alejandro那么您将失去设计时支持。有一些解决方案可以在生成代码时更改设计器的行为,包括注册新的
CodeDomeSerializer
。有关更多信息,请查看。但在实际行动中,正如我所提到的,如果不改变一些事情,就没有简单的方法来防止这种行为。对我来说,似乎是这样,而且你不应该对这个名字有任何依赖,无论如何,我为你的问题提供了答案,但是你可能不喜欢这个答案;)