Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用分部类和设计器文件将Visual Studio 2003表单转换为Visual Studio 2005/2008表单_C#_.net_Visual Studio - Fatal编程技术网

C# 使用分部类和设计器文件将Visual Studio 2003表单转换为Visual Studio 2005/2008表单

C# 使用分部类和设计器文件将Visual Studio 2003表单转换为Visual Studio 2005/2008表单,c#,.net,visual-studio,C#,.net,Visual Studio,将VisualStudio2003项目迁移到VS2005(或VS2008)后,我的表单仍然位于单个文件中 VS2005和VS2008上的新表单是使用分部类创建的,编辑器生成的所有代码都保存在Designer.cs文件中 由于VS2005表单创建是一种更好的处理表单的方法,我想知道是否有一种方法可以将所有旧的单文件表单转换为VS2005分部类方法 我已经手工做了一些,但这是非常棘手的,可能会导致一些严重的错误 有什么建议吗?PS:我正在使用Microsoft Visual C#2008Expres

将VisualStudio2003项目迁移到VS2005(或VS2008)后,我的表单仍然位于单个文件中

VS2005和VS2008上的新表单是使用分部类创建的,编辑器生成的所有代码都保存在Designer.cs文件中

由于VS2005表单创建是一种更好的处理表单的方法,我想知道是否有一种方法可以将所有旧的单文件表单转换为VS2005分部类方法

我已经手工做了一些,但这是非常棘手的,可能会导致一些严重的错误


有什么建议吗?PS:我正在使用Microsoft Visual C#2008ExpressEdition。

这似乎正是您想要的

:

NET 2.0引入了分部类,可在中启用“.designer”文件 VisualStudio2005及更高版本。也就是说,所有的视觉 设计器生成的代码(控件声明、InitializeComponent 方法等)可以保存在与常规代码分开的文件中。 在中打开.NET 1.x Visual Studio 2003 WinForms项目时 VisualStudio2005/2008将您的项目升级到.NET2.0 很好,但不幸的是,它没有将WinForms类迁移到 到新的“.designer”项目结构

起初我认为 这将是一个DXCore插件的工作(在其上的免费框架) CodeRush是构建的),因为它为插件提供了 可以用来抓取所有正确成员并移动它们的代码 转换到设计器文件中。在我调查这件事之前,我检查了一下 简单地将其实现为VisualStudio有哪些选项 宏。我完全希望必须使用正则表达式来 grep打开代码文件来执行任务,但感到惊喜 发现中的Visual Studio扩展性API可用于 宏提供了一个代码模型(我认为基于.NET CodeDom) 您可以遍历它来检查和操作底层代码。 下面是生成的“ExtractWinFormsDesignerFile”宏 是否:

  • 通过遍历 ProjectItem.FileCodeModel.CodeElements
  • 通过遍历CodeClass.Members从类中提取InitializeComponent和Dispose方法
  • 提取所有控件字段:即,其类型派生自System.Windows.Forms.control或System.ComponentModel.Container的所有字段 或者其类型名称以System.Windows.Forms开头
  • 将所有提取的代码放入新的“FormName.Designer.cs”文件中
这是目前唯一的C#–可以很容易地转换为 生成或修改的VB.NET代码正确使用FileCodeModel 在生成代码时,可能以与语言无关的方式创建代码 设计器文件。我在生成设计器时走了一条捷径 文件作为字符串,并将其直接写入文件

要“安装”: :

'-------------------------------------------------------------------------
'提取WinForms设计器文件Visual Studio 2005/2008宏
' -------------------------------------------------------------------------
'提取InitializeComponent()和Dispose()方法和控件
'从.NET1.xVS2003项目到VS2005/8项目的字段增量
'在*.Designer.cs文件中设置.NET 2.0部分类的样式。(目前为C#
"只有)
' 
“使用:
'*将下面的方法复制到Visual Studio宏模块中(使用
'ALT+F11以显示宏编辑器)
'*在解决方案资源管理器中选择一个Windows窗体
'*通过显示宏资源管理器(ALT+F8)和双击来运行宏
'单击'ExtractWinFormsDesignerFile'宏。
“*然后将提示您手动将表单类设置为部分:
'即更改“公共类MyForm:Form”
”“对
““公共部分类MyForm:Form”
'
邓肯·斯马特,《信息基础》,2007年
' -------------------------------------------------------------------------
子提取器WinFormsDesignerFile()
将项目设置为项目项=DTE。选择编辑项。项目(1)。项目项
Dim fileName As String=item.FileNames(1)
Dim dir As String=System.IO.Path.GetDirectoryName(文件名)
Dim bareName为String=System.IO.Path.GetFileNameWithoutExtension(文件名)
Dim newItemPath为String=dir&“\”&bareName&“.Designer.cs”
Dim codeClass As codeClass=findClass(item.FileCodeModel.codelements)
Dim namespaceName As String=codeClass.Namespace.FullName
出现错误时,请继续下一步“原谅我:-”)
Dim initComponentText As String=extractMember(codeClass.Members.Item(“InitializeComponent”))
Dim disposeText As String=extractMember(codeClass.Members.Item(“Dispose”))
Dim fieldDecls As String=extractWinFormsFields(编解码器类)
错误转到0
System.IO.File.WriteAllText(newItemPath,“”_
&“使用系统;”&vbCrLf_
&“使用System.Windows.Forms;”&vbCrLf_
&“使用System.Drawing;”&vbCrLf_
&“使用System.ComponentModel;”&vbCrLf_
&“使用System.Collections;”&vbCrLf_
&“”&vbCrLf_
&“名称空间”&namespaceName&vbCrLf_
&“{”&vbCrLf_
&“公共分部类”和编解码器类。名称和vbCrLf_
&“{”&vbCrLf_
&“#区域Windows窗体设计器生成的代码”&vbCrLf_
&“”&fieldDecls&vbCrLf_
&“”&initComponentText&vbCrLf_
&“#endregion”&vbCrLf&vbCrLf_
&“”&disposeText&vbCrLf_
&“}”&vbCrLf_
&“}”&vbCrLf_
)
Dim newProjItem作为项目
    ' -------------------------------------------------------------------------
    ' Extract WinForms Designer File Visual Studio 2005/2008 Macro
    ' -------------------------------------------------------------------------
    ' Extracts the InitializeComponent() and Dispose() methods and control
    ' field delarations from a .NET 1.x VS 2003 project into a VS 2005/8 
    ' style .NET 2.0 partial class in a *.Designer.cs file. (Currently C# 
    ' only)
    ' 
    ' To use: 
    '  * Copy the methods below into a Visual Studio Macro Module (use 
    '    ALT+F11 to show the Macro editor)
    '  * Select a Windows Form in the Solution Explorer
    '  * Run the macro by showing the Macro Explorer (ALT+F8) and double
    '    clicking the 'ExtractWinFormsDesignerFile' macro.
    '  * You will then be prompted to manually make the Form class partial: 
    '    i.e. change "public class MyForm : Form"
    '          to
    '             "public partial class MyForm : Form"
    '
    ' Duncan Smart, InfoBasis, 2007
    ' -------------------------------------------------------------------------

    Sub ExtractWinFormsDesignerFile()
        Dim item As ProjectItem = DTE.SelectedItems.Item(1).ProjectItem
        Dim fileName As String = item.FileNames(1)
        Dim dir As String = System.IO.Path.GetDirectoryName(fileName)
        Dim bareName As String = System.IO.Path.GetFileNameWithoutExtension(fileName)
        Dim newItemPath As String = dir & "\" & bareName & ".Designer.cs"

        Dim codeClass As CodeClass = findClass(item.FileCodeModel.CodeElements)
        Dim namespaceName As String = codeClass.Namespace.FullName

        On Error Resume Next ' Forgive me :-)
        Dim initComponentText As String = extractMember(codeClass.Members.Item("InitializeComponent"))
        Dim disposeText As String = extractMember(codeClass.Members.Item("Dispose"))
        Dim fieldDecls As String = extractWinFormsFields(codeClass)
        On Error GoTo 0

        System.IO.File.WriteAllText(newItemPath, "" _
          & "using System;" & vbCrLf _
          & "using System.Windows.Forms;" & vbCrLf _
          & "using System.Drawing;" & vbCrLf _
          & "using System.ComponentModel;" & vbCrLf _
          & "using System.Collections;" & vbCrLf _
          & "" & vbCrLf _
          & "namespace " & namespaceName & vbCrLf _
          & "{" & vbCrLf _
          & "   public partial class " & codeClass.Name & vbCrLf _
          & "   {" & vbCrLf _
          & "       #region Windows Form Designer generated code" & vbCrLf _
          & "       " & fieldDecls & vbCrLf _
          & "       " & initComponentText & vbCrLf _
          & "       #endregion" & vbCrLf & vbCrLf _
          & "       " & disposeText & vbCrLf _
          & "   }" & vbCrLf _
          & "}" & vbCrLf _
          )
        Dim newProjItem As ProjectItem = item.ProjectItems.AddFromFile(newItemPath)
        On Error Resume Next
        newProjItem.Open()
        DTE.ExecuteCommand("Edit.FormatDocument")
        On Error GoTo 0

        MsgBox("TODO: change your class from:" + vbCrLf + _
               "  ""public class " + codeClass.FullName + " : Form""" + vbCrLf + _
               "to:" + _
               "  ""public partial class " + codeClass.FullName + " : Form""")
    End Sub

    Function findClass(ByVal items As System.Collections.IEnumerable) As CodeClass
        For Each codeEl As CodeElement In items
            If codeEl.Kind = vsCMElement.vsCMElementClass Then
                Return codeEl
            ElseIf codeEl.Children.Count > 0 Then
                Dim cls As CodeClass = findClass(codeEl.Children)
                If cls IsNot Nothing Then
                    Return findClass(codeEl.Children)
                End If
            End If
        Next
        Return Nothing
    End Function

    Function extractWinFormsFields(ByVal codeClass As CodeClass) As String

        Dim fieldsCode As New System.Text.StringBuilder

        For Each member As CodeElement In codeClass.Members
            If member.Kind = vsCMElement.vsCMElementVariable Then
                Dim field As CodeVariable = member
                If field.Type.TypeKind <> vsCMTypeRef.vsCMTypeRefArray Then
                    Dim fieldType As CodeType = field.Type.CodeType
                    Dim isControl As Boolean = fieldType.Namespace.FullName.StartsWith("System.Windows.Forms") _
                       OrElse fieldType.IsDerivedFrom("System.Windows.Forms.Control") _
                       OrElse fieldType.IsDerivedFrom("System.ComponentModel.Container")
                    If isControl Then
                        fieldsCode.AppendLine(extractMember(field))
                    End If
                End If
            End If
        Next
        Return fieldsCode.ToString()
    End Function

    Function extractMember(ByVal memberElement As CodeElement) As String
        Dim memberStart As EditPoint = memberElement.GetStartPoint().CreateEditPoint()
        Dim memberText As String = String.Empty
        memberText += memberStart.GetText(memberElement.GetEndPoint())
        memberStart.Delete(memberElement.GetEndPoint())
        Return memberText
    End Function
<Compile Include="MyForm.Designer.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="MyForm.Designer.cs">
  <DependentUpon>MyForm.cs</DependentUpon>
</Compile>