現在已經將源碼放到GitHub中了
地址是
https://github.com/liningit/ViewJsLN
公司開發的項目使用的是Mvc框架,且Js與View頁面是分開在兩個文件夾下的,所以如果在View頁面轉到Js頁面或者反過來都要先找到頁面打開,很不方便.於是我做了一個VS外接程序可以很方便的用快捷鍵就直接互相跳轉.
公司網站目錄是如下圖所示:View目錄有Js分別在兩個文件夾下:Views與Scripts

- 先打開VS新建項目:VS外接程序

- 因為我是用C#開發的所以選擇Using C#

- 然后在下面這個圖選中創建Tool這個選項

- 打開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);
- 修改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; } } }
- 然后生成dll把MyAddinJSView.dll考到一個目錄下
- 打開MyAddinJSView - For Testing.AddIn文件所在的目錄,我的是在
C:\Users\Administrator\Documents\Visual Studio 2013\Addins下 - 修改 <Assembly>D:\LN\項目\MyAddinJSView\MyAddinJSView\bin\MyAddinJSView.dll</Assembly>為MyAddinJSView.dll的目錄
- 這時候在打開vs在工具下面又一個js互跳的菜單了,如果沒有則在工具>外接程序里面啟用一下試試看
- 然后打開工具>自定義,點擊下面的鍵盤按鈕

- 在顯示命令包含里面打myadd搜到我們新增的菜單設個快捷鍵並點擊分配按鈕就可以了

