Visual Studio中View页面与Js页面用快捷键互相跳转

现在已经将源码放到GitHub中了

地址是

https://github.com/liningit/ViewJsLN

公司开发的项目使用的是Mvc框架,且Js与View页面是分开在两个文件夹下的,所以如果在View页面转到Js页面或者反过来都要先找到页面打开,很不方便.于是我做了一个VS外接程序可以很方便的用快捷键就直接互相跳转.

公司网站目录是如下图所示:View目录有Js分别在两个文件夹下:Views与Scripts

  1. 先打开VS新建项目:VS外接程序
  2. 因为我是用C#开发的所以选择Using C#
  3. 然后在下面这个图选中创建Tool这个选项
  4. 打开Connect.cs文件修改OnConnection中的一行
       Command command = commands.AddNamedCommand2(_addInInstance, "MyAddinJSView", "JSView互跳", "JSView互跳", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
  5. 修改Exec方法如下
      public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
            {
                handled = false;
                if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
                {
                    if (commandName == "MyAddinJSView.Connect.MyAddinJSView")
                    {
                        handled = true;
                        try
                        {
                            var file = _applicationObject.ActiveDocument;
                            string opFile = string.Empty;
                            if (file.FullName.ToLower().EndsWith(".cshtml"))
                            {
                                opFile = file.FullName.ToLower().Replace(@"\views\", @"\scripts\").Replace(".cshtml", ".js");
                            }
                            else if (file.FullName.ToLower().EndsWith(".js"))
                            {
                                opFile = file.FullName.ToLower().Replace(@"\scripts\", @"\views\").Replace(".js", ".cshtml");
                            }
                            if (!string.IsNullOrEmpty(opFile))
                            {
                                _applicationObject.ItemOperations.OpenFile(opFile);
                            }
                        }
                        catch (Exception)
                        { 
                        }
                        return;
                    }
                }
            }
  6. 然后生成dll把MyAddinJSView.dll考到一个目录下
  7. 打开MyAddinJSView - For Testing.AddIn文件所在的目录,我的是在
    C:\Users\Administrator\Documents\Visual Studio 2013\Addins下
  8. 修改 <Assembly>D:\LN\项目\MyAddinJSView\MyAddinJSView\bin\MyAddinJSView.dll</Assembly>MyAddinJSView.dll的目录
  9. 这时候在打开vs在工具下面又一个js互跳的菜单了,如果没有则在工具>外接程序里面启用一下试试看
  10. 然后打开工具>自定义,点击下面的键盘按钮
  11. 在显示命令包含里面打myadd搜到我们新增的菜单设个快捷键并点击分配按钮就可以了

 

posted @ 2015-10-15 17:54  liningit  阅读(3111)  评论(2编辑  收藏  举报