斯普利特老.NET code到设计部分类 [英] Split old .NET code into designer partial class

查看:127
本文介绍了斯普利特老.NET code到设计部分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力具有所有设计code塞进同一个code文件作为我的code较旧的.NET code基(pre - 局部类)

I'm working on an older .NET code base that has all the designer code stuffed into the same code file as my code (pre - partial classes).

有没有一种机制来告诉Visual Studio 2008的要回去重构设计code到X.designer.cs部分类文件?

Is there a mechanism to tell Visual Studio 2008 to go back and refactor designer code into a X.designer.cs partial class file?

推荐答案

完全手动什么我所做的是:

What I just did was completely manually:

  1. 创建的 yourform 的了.Designer.cs
  2. 将其添加到Visual Studio(右键单击,添加现有的项目)
  3. 添加的部分的关键字,您现有的类。
  4. 添加的命名空间酷似原来的CS yourform 的了.Designer.cs
  5. 在这个命名空间中,添加的的定义(不要​​忘记包含的部分的关键字)。 不要添加继承和/或接口的Designer.cs这部分类。
  6. 在此过程完成后,你就可以的剪切粘贴的以下内容:
  1. Create yourform.Designer.cs
  2. Add it to Visual Studio (right click, add existing item)
  3. Add the partial keyword to your existing class.
  4. Add the namespace exactly like in the original cs to the yourform.designer.cs
  5. Inside this namespace, add the class definition (don't forget to include the partial keyword). Do not add inheritance and/or interfaces to this partial class in Designer.cs.
  6. After this is done, you're ready to cut and paste the following:

a)拆下的的组件的对象,你可能在你原来的WinForm的。如果应用程序是.NET 1.1,你会是这样的:

a) Remove the components object you might have in your original Winform. If the application was .NET 1.1 you will have something like this:

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private Container components = null;

b)增加一个新的的组件的在设计类对象:

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

C)除非你有一个的的具体的处理方法,这是标准。如果你没有任何形式的传承,我觉得base.Dispose可以安全删除:

c) Unless you had a specific dispose method, this is the standard. If you don't have any form Inheritance, I think that base.Dispose can be safety removed:

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose( bool disposing )
    {
        if ( disposing && ( components != null ) )
        {
            components.Dispose();
        }
        base.Dispose( disposing );
    }

D)复制所有 #region Windows窗体设计里面的code产生code 以新的Designer.cs类。

d) Copy all the code inside the #region Windows Form Designer generated code to the new Designer.cs class.

e)你也应该复制所有的成员变量的所有对象(标签,texboxes,等你在设计使用)。

e) You should also copy all the member variables for all your objects (labels, texboxes, etc. that you use in the designer).

这应该是有关它的一切。保存和编译。

That should be all about it. Save and compile.

这是值得? 好了,在我的情况,我有一堆的巨大的WinForms与吨code和控制。 VS2008抓取每一个我从/切换到设计人员的时间。这使得code查看更加敏感。我记得有一个具有响应code之前要等待3-5秒。现在只需要1 ...

Is it worthwhile? Well, in my case, I had a bunch of huge winforms with tons of code and controls. VS2008 crawled every time I switched from/to designer. This made the code view more responsive. I remember having to wait for 3-5 seconds before having a responsive code. Now it takes 1…

做的步骤1至5和移动现有的或添加一个新的控制将不会自动移动任何东西到designer.cs类。 新的东西去到新的设计类,但旧的东西仍然在那里它是,很遗憾。

Doing steps 1 to 5 and moving an existing or adding a new control won't automatically move anything to the designer.cs class. New stuff goes to the new Designer class, but old stuff remains where it was, unfortunately.

您还需要的代码的并重新打开该文件(你已经添加后/创建的部分级别)VS在设计正确绘制;被吸引如果不这么做,可能会导致空的形式。

You also have to close and reopen the file (after you have added/created the partial class) for VS to draw correctly in the Designer; failure to do may result in empty forms being drawn.

这篇关于斯普利特老.NET code到设计部分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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