通常情况下,可以在解决方案资源管理器中双击
Form1.cs
文件以访问简单的拖放设计器。不幸的是,它只会打开该文件的代码。
Form1.cs
文件以访问简单的拖放设计器。当在表单类之前放置另一个类时(在此情况下为ImageContainer
类),会出现相同的问题。
namespace ImageProcessor
{
internal class ImageContainer
{
}
public partial class Form1 : Form
{
private ImageContainer m_img = null;
}
}
internal class ImageContainer
移动到 public partial class Form1 : Form { }
下方,VS2019 拖拽设计器就能够正确加载我的 Form1 类。namespace ImageProcessor
{
public partial class Form1 : Form
{
private ImageContainer m_img = null;
}
internal class ImageContainer
{
}
}
解决方案资源管理器中的符号也被改回对话框图标。
Form1
。对于每个文件,您应该有类似以下条目的条目:<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
SubType
和DependentUpon
。这些是重要的部分。TargetFrameworkVersion
显示为v<Number>
。...
<OutputType>WinExe</OutputType>
<RootNamespace>HelloWorld</RootNamespace>
<AssemblyName>HelloWorld</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
...
与其使用下面的短 xml(*.Net Core),其中 TargetFramework
显示为 netcoreapp<Number>
:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
如果你想使用 .NET Core 项目,请不要忘记安装 .NET Core Windows Forms Designe,因为它还没有默认包含在 Visual Studio 中。请参阅上一个“启用设计者”部分。
另一种可能是您的表单类在 csproj 中的 xml 元素中缺少 SubType 子元素。
例如,这里有一个具有所需子元素的示例:
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
在其他地方找到了这个解决方案:
我在Visual Studio 2017中遇到了类似的问题。在“Form1.cs [Design]”上显示了一个错误,我关闭了它。然后我注意到Form1.cs没有“查看设计器”选项(图标也变成了常规类的图标)。幸运的是,我找出了错误所在:我在Form1.cs文件中将一个不是public partial class Form1的类放在了第一个类中。修复后,图标改变并重新启用了“查看设计器”选项。
很遗憾这种错误不会显示在错误列表中。我的错误很容易发现,但我知道这可能是一个问题,特别是在更大的项目中。