我試圖爲Visual Studio實現某種起始頁擴展。主要目的是通過在每次打開解決方案時啓動本地HTML文件來爲我工作的公司內的特定項目提供說明和最佳實踐。我開始使用完美工作的Visual Commander(https://vlasovstudio.com/visual-commander/extensions.html)。但我想把它變成一個VSIX文件。經過一番研究,我生成了代碼,但如果我調試或直接從調試文件夾安裝vsix,則不會發生任何事情(即使我在第一行中拋出異常)。代碼非常簡單:Visual Studio VSIX OnSolutionOpened無法正常工作
#region Package Members
DTE DTE;
/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
/// </summary>
protected override void Initialize()
{
base.Initialize();
try
{
IServiceContainer serviceContainer = this as IServiceContainer;
DTE = serviceContainer.GetService(typeof(SDTE)) as DTE;
EnvDTE.Events events = DTE.Events;
EnvDTE.SolutionEvents solutionEvents = events.SolutionEvents;
solutionEvents.Opened += OnSolutionOpened;
}
catch (Exception ex)
{
throw ex;
}
}
private void OnSolutionOpened()
{
try
{
string startupFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(DTE.Solution.FullName), GetSolutionStartPage());
if (System.IO.File.Exists(startupFile))
{
DTE.ItemOperations.Navigate(startupFile);
}
}
catch (Exception ex)
{
throw ex;
}
}
string GetSolutionStartPage()
{
return ((DTE.Solution != null) ? System.IO.Path.GetFileNameWithoutExtension(DTE.Solution.FullName) : "") + ".html";
}
#endregion
是的,這工作!我最終使用了[ProvideAutoLoad(UIContextGuids80.SolutionExists)],但本質上這種情況基本相同。 –