Visual Studio可扩展性:返回的IChangesExt服务始终为null

时间:2015-08-23 08:13:27

标签: c# visual-studio visual-studio-extensions

我正在尝试向我的Visual Studio 2015扩展程序Diff All Files添加Git支持。我已经为TFVC团队资源管理器页面正常工作了,我现在尝试将我的扩展程序显示在Git Changes和Git History页面上。为了能够在Git Changes页面上检测到挂起的更改,我需要获取IChangesExt服务的句柄。 This page shows how to do it,但由于某种原因,它对我不起作用;我总是只返回一个null值,而不是预期的IChangesExt实例。

以下是该网站的相关代码段:

Microsoft.TeamFoundation.Controls.ITeamExplorer teamExplorer;
teamExplorer = base.GetService(typeof(Microsoft.TeamFoundation.Controls.ITeamExplorer)) as Microsoft.TeamFoundation.Controls.ITeamExplorer;

Microsoft.TeamFoundation.Controls.ITeamExplorerPage teamExplorerPage;
Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesExt changesExt;

teamExplorerPage = teamExplorer.NavigateToPage(new Guid(pageGuid), null);
changesExt = teamExplorerPage.GetExtensibilityService(typeof(Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesExt)) as Microsoft.TeamFoundation.Git.Controls.Extensibility.IChangesExt;

我的代码略有不同,因为我的代码在打开Git Changes窗口时自动执行我不需要执行teamExplorer.NavigateToPage()调用。相反,我使用teamExplorer.CurrentPage,我在调试器中看到的实际上是Git Changes页面(GitPendingChangesPageVS是类型),所以我应该在网站的例子中进行相同的调用。这是我在Git Changes页面的Loaded()函数中的代码。请注意,我尝试以不同的方式获取IChangesExt服务,但所有这些都只返回null。

public override void Loaded(object sender, SectionLoadedEventArgs e)
{
    base.Loaded(sender, e);

    // Find the Pending Changes extensibility service and save a handle to it.
    var service = this.GetService<IChangesExt>();

    var teamExplorer = this.GetService<ITeamExplorer>();
    var service2 = teamExplorer.GetService(typeof(IChangesExt)) as IChangesExt;
    var service3 = teamExplorer.CurrentPage.GetExtensibilityService(typeof(IChangesExt)) as IChangesExt;
    var service4 = teamExplorer.CurrentPage.GetService<IChangesExt>();
...
}

任何想法我做错了什么?

扩展程序是开源的,所以如果您愿意,可以download the source from here(确保获得GitInitialFunctionality分支)并查看。相关代码位于GitChangesSection.cs文件中。

我还发布了this question on the MSDN forums,希望经常光顾这些论坛的人可以帮助我。

任何建议都表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了问题的根源。我只是注意到VS 2013中的一切都运行良好,但在VS 2015中没有。检查我的引用后,我看到我的VS 2015项目引用了2013(v12.0)Microsoft.TeamFoundation.Git.Controls.dll,这是包含IChangesExt接口的程序集。我将VS 2015(v14.0)版本的dll添加到我的解决方案中,并将我的VS 2015项目更改为引用它,现在它按预期工作:)