【问题标题】:How to write to Source Code from Custom Control Designer?如何从自定义控件设计器写入源代码?
【发布时间】:2020-01-27 00:42:46
【问题描述】:

我正在尝试创建一个用户控件,该控件将修改模板生成的一些源代码。

我已尝试从我创建的自定义 ControlDesign 中获取程序集位置,但这是 Visual Studio .exe 位置,而不是源代码位置。

        Private Designer As BulkOpsVesselGridControlDesigner

        Public Sub New(ByVal Designer As BulkOpsVesselGridControlDesigner)
            MyBase.New(Designer.Component)
            Me.Designer = Designer
        End Sub

        Public Property GridType() As BulkOpsGrids
            Get
                Return Me.Designer.VesselGridControl.GridType
            End Get
            Set(ByVal value As BulkOpsGrids)
                Me.Designer.VesselGridControl.GridType = value

                If value = BulkOpsGrids.NewGrid Then
                    'Display Input from Developer
                    Dim gridName = InputBox("Please Type out the name of the Grid Type Without Spaces. Ex: WorkingCrane")
                    'TODO: Get file path programmatically (based on developer)
                    Dim path = "C:\BulkOpsWinUILib\UserControls\GridPropertyData\BulkOpsGridData.vb"

                    Dim fileExists As Boolean = IO.File.Exists(path)
                    Using sw As New IO.StreamWriter(IO.File.Open(path, IO.FileMode.OpenOrCreate))
                        'TODO: write logic for formatting and proper placedment
                        sw.WriteLine(gridName)
                    End Using
                End If
            End Set
        End Property

最终,如果开发人员选择“newGrid”选项,我希望它自动提示网格定义的详细信息,然后将该选项添加到正确的源代码中。

希望创建一个模板,用于帮助标准化我们在未来应用程序中构建这些控件的方式。模板本身将帮助根据需要创建和构建源代码。类似于设计器如何自动写入 .designer 文件。

Image Of File Structure

【问题讨论】:

    标签: vb.net visual-studio custom-controls designer


    【解决方案1】:

    听起来好像您想从控件的设计器代码中自动化 Visual Studio。您可以使用IServiceProvider.GetService(Type) Method 获取对DTE Interface 的引用。组件的Site Property 实现了IServiceProvider 接口。

    获得 DTE 参考后,您可以检索有关当前加载的解决方案的信息并执行 VS 对象模型允许的任何操作,包括向项目添加新项目。

    下面的示例大致基于该问题,并显示了如何获取您所询问的各种路径信息。 smart-tag SomeProp 属性设置器会设置自定义 Label 的 text 属性以显示获取的信息。

    ' Project References:
    '   • System.Design
    '   • ENVDTE
    
    Imports System.ComponentModel
    Imports System.ComponentModel.Design
    Imports System.Windows.Forms.Design
    
    <Designer(GetType(TestControlDesigner))>
    Public Class TestControl : Inherits Label
      Public Sub New()
        AutoSize = True
      End Sub
    End Class
    
    Public Class TestControlDesigner : Inherits ControlDesigner
      Private _actionLists As DesignerActionListCollection
    
      Public Overrides ReadOnly Property ActionLists As DesignerActionListCollection
        Get
          If _actionLists Is Nothing Then
            _actionLists = New DesignerActionListCollection({New MyActions(Me.Component)})
          End If
          Return _actionLists
        End Get
      End Property
    
      Private Class MyActions : Inherits DesignerActionList
        Public Sub New(component As System.ComponentModel.IComponent)
          MyBase.New(component)
        End Sub
    
        Private _SomeProp As String
        Public Property SomeProp As String
          Get
            Return _SomeProp
          End Get
          Set(value As String)
            _SomeProp = value
            Dim serviceProvider As IServiceProvider = Me.Component.Site
            Dim dte As EnvDTE.DTE = CType(serviceProvider.GetService(GetType(EnvDTE.DTE)), EnvDTE.DTE)
            Dim doc As EnvDTE.Document = dte.ActiveDocument
            Dim proj As EnvDTE.Project = doc.ProjectItem.ContainingProject
            Dim cntrl As Control = CType(Me.Component, Control)
            Dim sb As New System.Text.StringBuilder(1000)
            sb.Append($"Document Path: {doc.Path} {Environment.NewLine}")
            sb.Append($"Document Fullname: {doc.FullName} {Environment.NewLine}")
            sb.Append($"Project Filename: {proj.Properties.Item("FileName").Value} {Environment.NewLine}")
            sb.Append($"Project File Path: {proj.Properties.Item("LocalPath").Value} {Environment.NewLine}")
            cntrl.Text = sb.ToString()
          End Set
        End Property
      End Class
    End Class
    

    【讨论】:

    • 这看起来正是我想要的。感谢您的回复,我希望尽快尝试!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多