Split old .NET code into designer partial class

2019-01-21 07:56发布

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).

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

7条回答
戒情不戒烟
2楼-- · 2019-01-21 08:21

Is the solution file of the older .NET code 2003/2005? I've tried to do what you're talking about by using the conversion wizard in VS2008. During conversion it should try to parse your .aspx pages and build designer files for them. What the designer generator has a problem with is that these older .aspx pages are so malformed that they simply can't be parsed.

I've come to the realization that perhaps trying to convert old ASP.NET code up to 2.0 or 3.5 is, in itself, going to be a large project. I have not yet found an effective way to factor out the designer code from old existing code, so I'd also be happy to learn of a solution here.

查看更多
爷的心禁止访问
3楼-- · 2019-01-21 08:23

I have had good luck with the free stand-alone application DeCodEx (Designer Code Extractor).

DeCodeEx loads in a Visual Studio project, detects which files are WinForms, and automatically splits designer code out of the original .cs or .vb file into a new .designer.cs or .designer.vb file.

For large projects it is much easier than trying to manually split out the files yourself.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-21 08:26

What I just did was completely manually:

  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) 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) Add a new components object in the Designer class:

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

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) Copy all the code inside the #region Windows Form Designer generated code to the new Designer.cs class.

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.

Remember that a partial class can be split among N number of files, but all must share the SAME namespace.

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…


UPDATE:

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.

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-21 08:34

I'd be interested to see what happens if you open one of these forms in the designer and save it; or modify it and save it; or do one of the above after creating a .designer.cs file with only an empty shell of the partial class. It wouldn't surprise me to find the designer placing at least the changes into the .designer.cs file.

查看更多
太酷不给撩
6楼-- · 2019-01-21 08:35

If anyone is still looking for an automated method of doing this there's a VS macro here that will do most of it for you.

A very nice tool.

查看更多
别忘想泡老子
7楼-- · 2019-01-21 08:36

To merge the yourForm.designer.cs to yourForm.cs, Follow the below steps

Edit .csproj in notepad and add the following code

<Compile Include="yourForm.designer.cs">
    <DependentUpon>yourForm.cs</DependentUpon>
</Compile>
查看更多
登录 后发表回答