Partial Form Class C# - 只显示类的代码视图 [英] Partial Form Class C# - Only display code view for class

查看:35
本文介绍了Partial Form Class C# - 只显示类的代码视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 C# 编写的空白表单项目.

I have a Blank Forms project in C#.

我想将函数和事件分离到表单类上的不同代码文件中.当它变大并且多人在 CodeControl 上使用它时,为了使其更易于管理.

I wanted to separate the functions and events into different Codefiles on the Form Class. In order to make it more manageable when it becomes large and multiple people are using it on CodeControl.

所以我创造了;

  • Form1.Functions.cs
  • Form1.Events.cs

这些额外的部分类都包含

These extra partial classes both consist of

namespace MyLargeProject
{
    public partial class Form1
    {

    } 

}

我还更改了 csproj 文件,以便 VS IDE 将它们显示在 Sotion Explorer 中.

I also changed the csproj file so the VS IDE would make them appear in the Soution Explorer.

示例

<Compile Include="Forms\Form1.Functions.cs">
  <DependentUpon>Form1.cs</DependentUpon>
  <SubType>Form</SubType>
</Compile>

所以程序编译后一切正常,只是当我从解决方案资源管理器打开文件时,它打开了一个空白的表单设计器,而不是代码.

So the program compiles and all is looking good except that when I open the file from the solution explorer it opens up a blank Form designer, and not the code.

我希望 VS 仅将此类作为代码文件打开以限制 Form1.Designer.cs 文件的问题.

I want VS to only open this class as a code file to limit issues with the Form1.Designer.cs file.

我也试过删除子类型

  <SubType>Form</SubType>

但 Visual Studio 不断将其添加回 csproj 文件.

but Visual Studio keeps adding it back to the csproj file.

推荐答案

正如评论中提到的,Visual Studio 将在代码编辑器中而不是在代码编辑器中打开任何以 .Designer.cs 结尾的文件表单设计师.利用这一点,在表单设计器中没有打开表单的一个很好的解决方案是命名包含以 .Designer.cs 结尾的类的文件.

As mentioned on the comments, Visual Studio will open any file ending with .Designer.cs in the code editor rather than on the forms designer. Taking advantage of this, a good solution to have a form not open in the form designer is to name the file that contains the class with ending .Designer.cs.

注意:我会说这并不优雅.我发现有一个名为 Form1.Designer.cs 的文件并且没有设计器是违反直觉的.此外,如果表单最初是使用表单设计器创建的,则需要合并 Form1.csForm1.Designer.cs,这在 Visual Studio 中可能会出现问题.

Note: I would say this is not elegant. I find it counter intuitive to have a file named Form1.Designer.cs and don't have a designer for it. Also, if the form was originally created with the forms designer, this will require to merge Form1.cs and Form1.Designer.cs which can be problematic from within Visual Studio.

我原来的回答

请按照以下步骤操作:

1) 确保设计器已关闭.

1) Make sure the designer is closed.

2) 修改您的 Form1.Designer.cs 并添加属性 DesignerCategory 和空字符串 ""(除设计器"、表单"或组件"也可以使用 - 不区分大小写):

2) Modify your Form1.Designer.cs and add the attribute DesignerCategory with the empty string "" (any random value other than "Designer", "Form" or "Component" also works - case insensitive):

namespace MyLargeProject
{
    [System.ComponentModel.DesignerCategory("")]
    public partial class Form1
    {
        // ...
    }
}

3) 卸载您的项目.

4) 重新加载您的项目.

4) Reload your project.

5) 注意设计器没有出现.

5) Notice the designer doesn't appear.

6) 享受吧!

注意:在 Visual Studio 2012 上测试,但基于 2005 年的讨论主题.

Note: tested on Visual Studio 2012, yet based on a discussion thread from 2005.

这篇关于Partial Form Class C# - 只显示类的代码视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆