![](/img/trans.png)
[英]How to inherit a generic form and open it in the Visual Studio designer?
[英]How to open form designer in Visual Studio?
我正在尝试在 Visual Studio 中用 C# 创建一个 Windows 窗体应用程序。
通常可以双击解决方案资源管理器中的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
{
}
}
解决方案资源管理器中的符号也变回了对话框图标。
文件之间的关联一定是被某种方式破坏了。 您可以手动编辑 .csproj 文件并进行更正。 搜索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
。 这些是重要的部分。
在我的情况下,右键单击 Form1.cs(带有正确的表单图标)并选择“视图设计器”仍然显示代码。
我找到了这个线程并意识到我在创建新项目时不小心选择了.Net Core而不是.NET Framework :
我应该能够在解决方案资源管理器面板的 .csproj 文件(.NET Framework)中看到这个长 xml,其中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 的Designer尚未默认发布:
如果您想使用 .NET Core 项目,请不要忘记安装 .NET Core Windows 窗体设计器,因为默认情况下它尚未在 Visual Studio 中提供。 请参阅前面的“启用设计器”部分。
不知何故 .resx 文件损坏了,我通过从我的其他表单复制相同的文件来修复它。 只需删除损坏的,粘贴它,然后更改名称
在 VS2019 中,只需删除第一个项目并重新创建另一个项目。 它将解决查看设计器页面的问题。 现在您可以查看您的设计器。
在其他地方找到了这个解决方案:
我刚刚在 Visual Studio 2017 中遇到了类似的问题。在“Form1.cs [Design]”上显示了一个错误,我将其关闭。 然后我注意到 Form1.cs 没有“视图设计器”选项(图标也更改为普通类的图标)。 幸运的是,我发现了错误所在:我将公共部分类 Form1 以外的类作为第一类放在 Form1.cs 文件中。 修复导致图标更改并重新启用“视图设计器”选项的问题。
可惜那些错误没有显示在错误列表中。 我的错误很容易被发现,但我知道这可能是个问题,尤其是在更大的项目中。
另一种可能性是您的表单类在 csproj 的 xml 元素中缺少 SubType 子元素。
例如,这里有一个确实有所需的子元素:
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
在我的情况下,问题是cs文件名中的一个点,例如:“Form1.Core.cs”
实际上,您正在从文件中打开设计。 只需运行程序,它会自动打开设计表格。如果有内置错误,请忽略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.