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搜到我們新增的菜單設個快捷鍵並點擊分配按鈕就可以了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM