Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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
Asp.net mvc 在Visual Studio中插入替换字符的最快方法_Asp.net Mvc_Visual Studio_Visual Studio 2008_Keyboard Shortcuts - Fatal编程技术网

Asp.net mvc 在Visual Studio中插入替换字符的最快方法

Asp.net mvc 在Visual Studio中插入替换字符的最快方法,asp.net-mvc,visual-studio,visual-studio-2008,keyboard-shortcuts,Asp.net Mvc,Visual Studio,Visual Studio 2008,Keyboard Shortcuts,我刚刚开始学习ASP.NETMVC,发现自己在视图中写了很多东西。Intellisense确实提供了结束%>,但我发现键入介绍性的我认为符合要求。我发现直接编写宏,然后将其绑定到键盘命令 我使用工具->宏->宏资源管理器查看其中的内容,您可以创建一个新模块并添加一个宏来注入代码。然后用工具->自定义->键盘将其绑定到一个键上 因为它和您正在做的并没有太大的不同,所以这里有一个宏来注入一个带有日期和用户名的源命令-VBScript-我没有仔细寻找其他替代方法 Imports System Impo

我刚刚开始学习ASP.NETMVC,发现自己在视图中写了很多东西。Intellisense确实提供了结束%>,但我发现键入介绍性的我认为符合要求。

我发现直接编写宏,然后将其绑定到键盘命令

我使用工具->宏->宏资源管理器查看其中的内容,您可以创建一个新模块并添加一个宏来注入代码。然后用工具->自定义->键盘将其绑定到一个键上

因为它和您正在做的并没有太大的不同,所以这里有一个宏来注入一个带有日期和用户名的源命令-VBScript-我没有仔细寻找其他替代方法

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Module1

    Private Function GetUserName() As String
        GetUserName = System.Environment.UserName
    End Function

    Sub InjectChangeComment()
        ActiveDocument().Selection().Text = "// " + System.DateTime.Now.ToString("MM-dd-yy") + " " + GetUserName() + vbTab + vbTab + vbTab
    End Sub

End Module

根据一条评论,我仔细检查了下面的代码片段答案,不幸的是它没有在HTML视图中运行。另一种方法是通过录制的宏:

  • 在web项目中,开始录制:CTRL+SHIFT+R
  • 键入
    ,然后将插入符号返回到“=”后面的空格之间
  • 停止录制:CTRL+SHIFT+R
  • 通过CTRL+SHIFT+P插入宏
这可能足够了,但最好在所有项目中都使用它,另外,我们希望使用比CTRL+SHIFT+p更好的按键:

  • 保存宏:工具->宏->保存临时宏,并为其命名
  • 将其绑定到按键组合:
    • 工具->选项,然后选择键盘节点
    • 搜索您选择的名称
    • 输入组合键(例如ALT+a)并单击“确定”
现在,您可以在HTML视图中按快捷键(例如ALT+A),它将插入插入符号,并将插入符号放置在标记中,以便输入


[旧答案:不幸的是,在HTML视图中不起作用。]

对于代码段,创建一个包含名称、快捷方式和扩展名的XML代码段文件(例如“asp.Snippet”),然后使用工具->代码段管理器添加存储代码段的文件夹

以下是(通过“asp[tab][tab]”展开的代码段的XML


ASP服务器标签


顺便说一句,VS有一个代码段来创建代码段。只需打开一个新的XML文件,然后右键单击并选择Insert Snippet->“Snippet”。

此宏函数应该可以:

主代码将执行以下两项操作之一:如果未选择任何内容,则只插入代码结构;如果编辑器中当前选择了某个内容,则将使用结构包装该代码,例如

Public Sub-WrapMVC()
尝试
DTE.UndoContext.Open(“包装MVC”)
Dim OutText As String=“”
Dim OutFormat As String=“”
DTE.ActiveDocument.Selection.Text=String.Format(OutFormat,ActiveWindowSelection)
最后
DTE.UndoContext.Close()
结束尝试
端接头
助手例程:

Friend Function ActiveWindowSelection() As String
    If DTE.ActiveWindow.ObjectKind = EnvDTE.Constants.vsWindowKindOutput Then
        Return OutputWindowSelection()
    End If
    If DTE.ActiveWindow.ObjectKind = "{57312C73-6202-49E9-B1E1-40EA1A6DC1F6}" Then
        Return HTMLEditorSelection()
    End If
    Return SelectionText(DTE.ActiveWindow.Selection)
End Function

Private Function HTMLEditorSelection() As String
    Dim hw As EnvDTE.HTMLWindow = ActiveDocument.ActiveWindow.Object
    Dim tw As TextWindow = hw.CurrentTabObject
    Return SelectionText(tw.Selection)
End Function

Private Function OutputWindowSelection() As String
    Dim w As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    Dim ow As OutputWindow = w.Object
    Dim owp As OutputWindowPane = ow.OutputWindowPanes.Item(ow.ActivePane.Name)
    Return SelectionText(owp.TextDocument.Selection)
End Function

Private Function SelectionText(ByVal sel As EnvDTE.TextSelection) As String
    If sel Is Nothing Then
        Return ""
    End If
    If sel.Text.Length <= 2 Then
        SelectWord(sel)
    End If
    If sel.Text.Length <= 2 Then
        Return ""
    End If
    Return sel.Text
End Function

Private Sub SelectWord(ByVal sel As EnvDTE.TextSelection)
    Dim leftPos As Integer
    Dim line As Integer
    Dim pt As EnvDTE.EditPoint = sel.ActivePoint.CreateEditPoint()

    sel.WordLeft(True, 1)
    line = sel.TextRanges.Item(1).StartPoint.Line
    leftPos = sel.TextRanges.Item(1).StartPoint.LineCharOffset
    pt.MoveToLineAndOffset(line, leftPos)
    sel.MoveToPoint(pt)
    sel.WordRight(True, 1)
End Sub
Friend函数ActiveWindowSelection()作为字符串
如果DTE.ActiveWindow.ObjectKind=EnvDTE.Constants.vsWindowKindOutput,则
返回OutputWindowSelection()
如果结束
如果DTE.ActiveWindow.ObjectKind=“{57312C73-6202-49E9-B1E1-40EA1A6DC1F6}”,则
返回HTMLEditorSelection()
如果结束
返回SelectionText(DTE.ActiveWindow.Selection)
端函数
私有函数HTMLEditorSelection()作为字符串
作为EnvDTE.HTMLWindow=ActiveDocument.ActiveWindow.Object的Dim hw
Dim tw As TextWindow=hw.CurrentTabObject
返回SelectionText(tw.Selection)
端函数
私有函数OutputWindowSelection()作为字符串
Dim w As Window=DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
作为输出窗口调整ow=w.对象
Dim owp As OutputWindowPane=ow.OutputWindowPanes.Item(ow.ActivePane.Name)
返回SelectionText(owp.TextDocument.Selection)
端函数
私有函数SelectionText(ByVal sel作为EnvDTE.TextSelection)作为字符串
如果sel什么都不是,那么
返回“”
如果结束

如果HTML视图中的sel.Text.Length代码片段不起作用。它被安排在Visual Studio的下一个版本中。现在我想看看宏方法,或者看看其他工具是否允许在HTML编辑器中使用代码片段。

一个很好的工具是Resharper。您可以创建自己的模板来完成所需的工作,但也可以使用环绕标签。它有很多功能,而且价格合理。

谢谢Chris。我发现除非我将语言更改为“XML”,否则无法将其作为XML片段安装?即使在那之后,我也不会在编辑.ASPX页面时显示任何代码片段?如果我编辑一个XML文件,它就会提供。但是asp没有调用它;不过菜单可以。很抱歉,德克。我已经用基于宏的方法更新了答案。还是很简单,应该是你要找的。谢谢你,布莱恩。这也很好地工作,并且具有处理选定文本的优势。
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Module1

    Private Function GetUserName() As String
        GetUserName = System.Environment.UserName
    End Function

    Sub InjectChangeComment()
        ActiveDocument().Selection().Text = "// " + System.DateTime.Now.ToString("MM-dd-yy") + " " + GetUserName() + vbTab + vbTab + vbTab
    End Sub

End Module
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0"  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>ASP Server Tags</Title>
    <Author>Chris Bowen</Author>
    <Shortcut>asp</Shortcut>
    <Description>ASP.NET server escape characters, including equals</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>code</ID>
        <Default>Code</Default>
      </Literal>
    </Declarations>
    <Code Language="csharp">
      <![CDATA[<%= $code$ $selected$%>$end$]]>
    </Code>
  </Snippet>
</CodeSnippet>
Public Sub WrapMVC()
    Try
        DTE.UndoContext.Open("Wrap MVC")
        Dim OutText As String = ""
        Dim OutFormat As String = "<%={0} %>"
        DTE.ActiveDocument.Selection.Text = String.Format(OutFormat, ActiveWindowSelection)
    Finally
        DTE.UndoContext.Close()
    End Try
End Sub
Friend Function ActiveWindowSelection() As String
    If DTE.ActiveWindow.ObjectKind = EnvDTE.Constants.vsWindowKindOutput Then
        Return OutputWindowSelection()
    End If
    If DTE.ActiveWindow.ObjectKind = "{57312C73-6202-49E9-B1E1-40EA1A6DC1F6}" Then
        Return HTMLEditorSelection()
    End If
    Return SelectionText(DTE.ActiveWindow.Selection)
End Function

Private Function HTMLEditorSelection() As String
    Dim hw As EnvDTE.HTMLWindow = ActiveDocument.ActiveWindow.Object
    Dim tw As TextWindow = hw.CurrentTabObject
    Return SelectionText(tw.Selection)
End Function

Private Function OutputWindowSelection() As String
    Dim w As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    Dim ow As OutputWindow = w.Object
    Dim owp As OutputWindowPane = ow.OutputWindowPanes.Item(ow.ActivePane.Name)
    Return SelectionText(owp.TextDocument.Selection)
End Function

Private Function SelectionText(ByVal sel As EnvDTE.TextSelection) As String
    If sel Is Nothing Then
        Return ""
    End If
    If sel.Text.Length <= 2 Then
        SelectWord(sel)
    End If
    If sel.Text.Length <= 2 Then
        Return ""
    End If
    Return sel.Text
End Function

Private Sub SelectWord(ByVal sel As EnvDTE.TextSelection)
    Dim leftPos As Integer
    Dim line As Integer
    Dim pt As EnvDTE.EditPoint = sel.ActivePoint.CreateEditPoint()

    sel.WordLeft(True, 1)
    line = sel.TextRanges.Item(1).StartPoint.Line
    leftPos = sel.TextRanges.Item(1).StartPoint.LineCharOffset
    pt.MoveToLineAndOffset(line, leftPos)
    sel.MoveToPoint(pt)
    sel.WordRight(True, 1)
End Sub