Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.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
Vb.net 如何从自定义控件设计器写入源代码?_Vb.net_Visual Studio_Custom Controls_Designer - Fatal编程技术网

Vb.net 如何从自定义控件设计器写入源代码?

Vb.net 如何从自定义控件设计器写入源代码?,vb.net,visual-studio,custom-controls,designer,Vb.net,Visual Studio,Custom Controls,Designer,我正在尝试创建一个用户控件,该控件将修改模板生成的一些源代码 我已尝试从创建的自定义控件设计中获取程序集位置,但这是Visual Studio.exe位置,而不是源代码位置 Private Designer As BulkOpsVesselGridControlDesigner Public Sub New(ByVal Designer As BulkOpsVesselGridControlDesigner) MyBase.New(Des

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

我已尝试从创建的自定义控件设计中获取程序集位置,但这是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文件的方式


听起来好像您想从控件的设计器代码中自动化Visual Studio。您可以使用获取对的引用。组件的实现了IServiceProvider接口

一旦有了DTE引用,就可以检索有关当前加载的解决方案的信息,并执行VS对象模型允许的任何操作,包括向项目添加新项

下面的示例大致基于这个问题,并展示了如何获取您所询问的各种路径信息。智能标记SomeProp属性设置器将设置自定义标签的文本属性以显示获得的信息

' 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
”项目参考:
“•系统设计
“•环境
导入System.ComponentModel
导入System.ComponentModel.Design
导入System.Windows.Forms.Design
公共类TestControl:继承标签
公共分新()
自动调整大小=真
端接头
末级
公共类TestControlDesigner:继承ControlDesigner
作为DesignerActionListCollection的私有\u操作列表
Public将只读属性ActionList重写为DesignerActionListCollection
得到
如果行动清单什么都不是,那么
_ActionList=New DesignerActionListCollection({New MyActions(Me.Component)})
如果结束
返回操作列表
结束
端属性
私有类MyActions:继承DesignerActionList
Public Sub New(组件作为System.ComponentModel.IComponent)
MyBase.New(组件)
端接头
Private\u SomeProp作为字符串
公共属性SomeProp作为字符串
得到
返回某物
结束
设置(值为字符串)
_SomeProp=值
Dim serviceProvider作为IServiceProvider=Me.Component.Site
将dte设置为EnvDTE.dte=CType(serviceProvider.GetService(getype(EnvDTE.dte)),EnvDTE.dte)
作为EnvDTE.Document=dte.ActiveDocument的Dim文档
尺寸项目作为EnvDTE.Project=doc.ProjectItem.ContainingProject
Dim cntrl As Control=CType(Me.Component,Control)
将sb设置为新系统.Text.StringBuilder(1000)
sb.Append($“文档路径:{doc.Path}{Environment.NewLine}”)
sb.Append($“文档全名:{doc.Fullname}{Environment.NewLine}”)
sb.Append($“项目文件名:{proj.Properties.Item(“文件名”).Value}{Environment.NewLine}”)
sb.Append($“项目文件路径:{proj.Properties.Item(“LocalPath”).Value}{Environment.NewLine}”)
cntrl.Text=sb.ToString()
端集
端属性
末级
末级

这看起来正是我想要的。我很感激你的回复,我希望能尽快尝试!非常感谢你!