简体   繁体   English

局部类并双击控件

[英]Partial Classes and double-click on control

If I have a function in a partial class (let's say I have 'Button 2' click handler) and then double-click on 'Button 2' in the Form designer, Intellisense (or whatever is in charge now) always throws me to the 'Form1.cs module' (at an ill-defined or blank line), not the new partial class module 'Button2.cs'. 如果我在部分类中有一个函数(例如,我有“按钮2”单击处理程序),然后在窗体设计器中双击“按钮2”,则Intellisense(或现在负责的任何事情)总是让我陷入困境。 “ Form1.cs模块”(定义不正确或空白行),而不是新的局部类模块“ Button2.cs”。 Double-clicking on 'Button1' does the 'correct' action. 双击“ Button1”执行“正确”操作。

For example: 例如:

In 'Form1.cs': 在“ Form1.cs”中:

namespace test01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button 1 pressed");
        }

    }
}

In 'Button2.cs' (a public partial class): 在“ Button2.cs”(公共局部类)中:

namespace test01
{
    public partial class Form1 : Form
    {


        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button 2 pressed");
        }
    }
}

This is something I've seen in VS2008 and now in VS2010 and there is no obvious way (to me) to resolve it. 这是我在VS2008和现在的VS2010中看到的,并且(对我而言)没有明显的解决方法。

Any help out there? 有什么帮助吗?

That's because you are designing the form , not the button. 那是因为您正在设计表单 ,而不是按钮。 The designer expects that (by convention) all of your event handlers for the controls on the form are located in the form's source code, not the button's source code. 设计人员希望(按照约定)您在窗体上控件的所有事件处理程序都位于窗体的源代码中,而不是按钮的源代码中。 This makes sense when you consider that (generally) button2.cs probably should define a class called button2 , not an instance of the class Button whose Name property happens to be "Button2". 当您考虑到(通常)button2.cs可能应该定义一个名为button2类时 ,而不是其Name属性恰好是“ Button2”的Button类的实例时,这才有意义。

EDIT: 编辑:

Partial classes are designed for a very specific purpose- when a class is generated by a designer or other code generator, it is typically unsafe for a developer to edit that class by hand (because the code generator will come by later and overwrite the file, including the developer's hand-written parts). 部分类是为特定目的而设计的-当类是由设计人员或其他代码生成器生成的时,开发人员通常无法手动编辑该类(因为代码生成器稍后会覆盖文件,包括开发人员的手写部分)。 Partial classes allow a class to be split across two files- one is generated (by the designer in this case) and one is meant for hand-written code. 部分类允许将一个类划分为两个文件-一个生成(在这种情况下由设计人员生成),另一个则用于手写代码。 Because the class is defined as "partial", the compiler knows that other parts of the class may be defined in other files. 因为该类被定义为“部分”,所以编译器知道该类的其他部分可以在其他文件中定义。

For a project that has several different areas of functionality like you describe, I'd organize those into classes that were unrelated to the UI (so that the functionality can be independently unit tested), and then call them from UI classes as needed. 对于具有您所描述的功能的几个不同领域的项目,我将它们组织到与UI无关的类中(以便可以对功能进行独立的单元测试),然后根据需要从UI类中调用它们。 You should look into concepts like the Model-View-Controller or Model-View-ViewModel design patterns for an idea of how this works. 您应该研究诸如Model-View-ControllerModel-View-ViewModel设计模式之类的概念,以了解其工作原理。

A folder structure like this wouldn't be unreasonable as a starting point: 像这样的文件夹结构不会毫无道理:

Root
|-GUI           // Contains folders related to GUI elements
| |-Forms       // Contains your forms
| |-Controls    // Contains any custom controls
|-Common        // Contains folders for common functionality
| |-IO          // Contains classes relating to I/O
| |-Diagnostics // Contains classes relating to diagnostics
|-Logic         // Contains classes folders to specific business use cases
  |-UseCase1    // Contains classes folders to use case #1 (use a better name, obviously)
  |-UseCase2    // Contains classes folders to use case #2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM