Visual Studio设计视图 - 表单为空白

Dan*_*cik 8 c# winforms visual-studio-2012

我有一个带有两种形式的C#项目.所有编译都很好.当我运行项目时,所有表单都按预期绘制.

几天前,所有表单的DesignView 开始只显示一个空白表单,就像你在新的Windows.Forms项目中得到的表单一样.

我在SO上遇到了类似问题的几个问题,以下是对这些问题的评论:

  1. 我的项目没有第三方库(除了htmlAgilityPack,在其他Windows.Forms C#项目中不会导致此问题)
  2. 我已经检查过每个表单的InitializeComponent函数在项目中只有一次
  3. 当我创建一个新项目并添加一个现有表单(即我的一个有问题的项目表单)时,Design Viewer按预期工作 - 因此我怀疑我的表单的.cs,.Designer.cs和.resx文件是正常的.

在项目设置或其他地方有什么我可以搞砸的吗?

编辑

上面的第三点是误导性的 - 我也尝试为第二种形式创建一个新项目,并且问题仍然存在.显示问题的最少量源代码可在此处找到.

stu*_*rtd 18

您的项目文件已无效.

表单的有效项目条目如下所示:

<Compile Include="Form1.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
  <DependentUpon>Form1.cs</DependentUpon>
</Compile>
Run Code Online (Sandbox Code Playgroud)

虽然您缺少DependentUpon行 - 这就是代码和设计器文件在项目中单独出现而不是连接的原因:

<Compile Include="mainForm.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="mainForm.Designer.cs" />
Run Code Online (Sandbox Code Playgroud)

如果添加缺失的行,表单将在设计模式下正确显示:

<Compile Include="mainForm.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="mainForm.Designer.cs">
  <DependentUpon>mainForm.cs</DependentUpon>
</Compile>
Run Code Online (Sandbox Code Playgroud)

并整理资源文件:

<EmbeddedResource Include="mainForm.resx">
  <DependentUpon>mainform.cs</DependentUpon>
</EmbeddedResource>
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,您只需在编辑器中编辑csproj,或者在Visual Studio中执行此操作:

  1. 备份文件
  2. 右键单击该项目,然后选择"卸载项目"
  3. 右键单击卸载的项目,然后选择"编辑[ProjectName] .csproj"
  4. 进行更改,然后关闭文件.
  5. 右键单击卸载的项目,然后选择"重新加载项目"