Visual Studio 中的表单无法使用表单设计器

Form designer has became unavailable for a form in Visual Studio

在 Visual Studio 2015 年对表单进行了一些工作后,表单设计器突然无法使用。

症状:

为什么 Visual Studio 没有将表单文件评估为有效,没有显示设计器视图?

我不小心把自己锁在了门外,锁着的门后面写着错误信息。

问题:工作中,临时在表单代码文件开头添加了一个琐碎的帮手class。 如果我当时打开设计器,我可能会看到以下错误:

The class Form1 can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.

要注意的是,错误仅显示在设计器中。如果您此时只有代码 window 打开,您将不会发现错误 – 更糟糕的是,您不再被允许进入设计器视图来发现它。

所以当 Visual Studio 没有提供任何线索时,可能会发生这种情况 – 唯一的帮助是合格的猜测和试验。

万一其他人需要不同的修复,我遇到了同样的问题,但它与 telerik 报告有关。

不确定原因或方式,但我的问题是项目文件已损坏。报告缺少子类型元素。此元素告诉 visual studio 使用设计器模板而不是代码视图打开文件。

子类型取决于它是什么类型的控件。对于 windows 表单,子类型为 Form

卸载项目 -> 右键单击​​ -> 编辑“.csproj”

所以我改变了这个(在项目文件中):

<Compile Include="Report1.cs" />
<Compile Include="Report1.Designer.cs">
  <DependentUpon>Report1.cs</DependentUpon>
</Compile>

对此:

<Compile Include="Report1.cs">
  <SubType>Component</SubType> <!-- if winform, this would be Form instead -->
</Compile>
<Compile Include="Report1.Designer.cs">
  <DependentUpon>Report1.cs</DependentUpon>
</Compile>

重新加载项目。

只需从目录中添加现有的表单设计器即可解决问题。 在 visual studio 中添加现有文件后,显示 'view designer' 的上下文菜单。