[英]A partial class has multiple form
When I write a winforms application, I tend to create an Implementation.cs file for each form. 当我编写winforms应用程序时,我倾向于为每个表单创建一个Implementation.cs文件。
Say I have Form1.cs , I'd create a new file called Form1.Implementation.cs starting with partial class Form1
. 假设我有Form1.cs ,我将创建一个名为Form1.Implementation.cs的新文件,从
partial class Form1
。
Form1.cs just contains all the event callback methods (what the designer has done), everything else goes to Form1.Implementation.cs . Form1.cs只包含所有事件回调方法(设计人员已完成的工作),其他所有内容都包含在Form1.Implementation.cs中 。 It helps me write more readable code.
它帮助我编写更易读的代码。
I wanted Form1.Implementation.cs to be a "subfile" just like Form1.Designer.cs is, so I edited .csproj file. 我希望Form1.Implementation.cs是一个“子文件”,就像Form1.Designer.cs一样,所以我编辑了.csproj文件。
<Compile Include="Form1.Implementation.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
After reload, however, Visual Studio automatically adds <SubType>Form</SubType>
right after DependentUpon
element. 但是,重新加载后, Visual Studio会自动在
DependentUpon
元素之后添加<SubType>Form</SubType>
。 Doubleclicking Form1.Implementation.cs doesn't show code but a designer with another initial empty form. 双击Form1.Implementation.cs不显示代码,而是显示另一个初始空表单的设计器。
It's like 就像是
"class Form1 , which ISA Form , is(?) multiple forms."
“Form1类,其中ISA 表 ,是(?)多种形式。”
I tried adding DesignerCategory
attribute to class Form1 , but it affects Form1.cs , as well. 我尝试将
DesignerCategory
属性添加到类Form1 ,但它也影响Form1.cs 。
Well, hitting 'Shift-F7' or 'Ctrl-Shift-0' is not a big deal. 好吧,点击'Shift-F7'或'Ctrl-Shift-0'并不是什么大问题。 I wonder if...
我怀疑是否...
The only way of achieving what you tried is by adding <DependentUpon>
, which you already know. 实现您尝试的方法的唯一方法是添加您已经知道的
<DependentUpon>
。 Now Visual Studio automatically adds the <SubType>Form</SubType>
for any class derived from Form . 现在,Visual Studio会自动为从Form派生的任何类添加
<SubType>Form</SubType>
。 Since your Form1.cs probably contains the line public partial class Form1 : Form
, this is where the SubType is coming from. 由于您的Form1.cs可能包含行
public partial class Form1 : Form
,因此这是SubType的来源。
The other files - Form1.Designer.cs and Form1.Implementation.cs may contain only partial class Form1
, but since partial class definitions across multiple files are still effectively one class definition, Visual Studio detects that it still inherits Form . 其他文件 - Form1.Designer.cs和Form1.Implementation.cs可能只包含
partial class Form1
,但由于多个文件中的部分类定义仍然是一个类定义,因此Visual Studio检测到它仍然继承了Form 。 I believe you may already know that, but just in case here's the MSDN article about the partial keyword . 我相信你可能已经知道了,但以下是关于partial关键字的MSDN文章 。 Don't worry about there being multiple instances of Form in this scenario.
不要担心在这种情况下有多个Form实例。 Remember this still is just one class -
Form1
, no mater over how many files you spread it. 请记住,这仍然只是一个类 -
Form1
,不管你传播多少个文件。
In the end, all code files containing classes (even partial!) inheriting Form (or UserControl ) are automatically opened in the Designer. 最后,包含继承Form (或UserControl )的类(甚至部分!)的所有代码文件都会在Designer中自动打开。 This behaviour is by design.
此行为是设计使然。
The solution here is simple - either make a code file defining a separate class not based on Form , or just use F7 to view the code of that file in Solution Explorer, however annoying that may seem. 这里的解决方案很简单 - 要么创建一个不基于Form的单独类的代码文件,要么只使用F7在解决方案资源管理器中查看该文件的代码,但这看起来很烦人。 It doesn't matter if that code file is
<DependentUpon>
anything or not. 如果该代码文件是
<DependentUpon>
则无关紧要。 Only the inheritance of Form or UserControl matters. 只有Form或UserControl的继承很重要。
The best solution though, in my opinion, would be to stick to what Visual Studio is giving you: 在我看来,最好的解决方案是坚持Visual Studio给你的东西:
This is an approach my team has been sticking to for a few years now. 这是我的团队已经坚持了几年的方法。 It provides a basic means of separation of Designer code and hand-coded actions.
它提供了分离Designer代码和手动编码操作的基本方法。 To better separate your code, use a design pattern such as MVP, as suggested by Simon Whitehead in the comments.
为了更好地分离您的代码,请使用Simon Whitehead在评论中建议的设计模式,如MVP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.