简体   繁体   English

如何在 C# 中以编程方式订阅 TFS 的 VersionControlServer 事件?

[英]How to subscribe to TFS's VersionControlServer Events programatically in C#?

I'm writing an Visual Studio 2015 Extension, where i want to subscribe to some of the TFS's VersionControlServer Events.我正在编写一个 Visual Studio 2015 扩展,我想在其中订阅一些 TFS 的 VersionControlServer 事件。 But actually none of the events getting fired.但实际上没有一个事件被解雇。 I guess i already know why, but I dont know how to get it working for my purpose.我想我已经知道为什么了,但我不知道如何让它为我的目的工作。 Also the documentation from microsoft is pretty poor and literally non-existend ...微软的文档也很差,字面上不存在......

The question is: Is there a way to subscribe to the events from my extension.问题是:有没有办法从我的扩展订阅事件。

I've already researched many threads and couldn't find anything that is helpful for me.我已经研究了很多线程,但找不到任何对我有用的东西。 My code looks like this:我的代码如下所示:

var tfsProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection (TfsPath, new UICredentialsProvider ());
var tfsVersionControlServer = tfsProjectCollection.GetService<VersionControlServer> ();

tfsVersionControlServer.CommitCheckin += TfsVersionControlServer_CommitCheckin;

The problem here is , that the VersionControlServer is not the same instance that the Visual Studio uses.这里的问题是,VersionControlServer 与 Visual Studio 使用的实例不同。 So if I, for example, make a shelveset, the event wont fire.因此,例如,如果我制作搁置集,则该事件不会触发。 Only if I would do the shelving programatically with the instance I have here, I would get the event fired.只有当我使用这里的实例以编程方式进行搁置时,我才会触发该事件。

My extension is working fine, except this issue.我的扩展工作正常,除了这个问题。 My extension is getting initialized on startup and subscribes during Visual Studio startup to the events.我的扩展在启动时初始化并在 Visual Studio 启动期间订阅事件。 Now if I create Shelveset I would like to have the event BeforeShelvePendingChange fired.现在,如果我创建 Shelvesset,我希望触发 BeforeShelvePendingChange 事件。

Thanks in advance提前致谢

As I already said, I got a solution for my problem (with some drawbacks).正如我已经说过的,我为我的问题找到了解决方案(有一些缺点)。

Lets say my extension name is MyExtension:假设我的扩展名是 MyExtension:

First I added a Property in MyExtension.cs首先我在 MyExtension.cs 中添加了一个属性

 public EnvDTE.DTE DTEObject { get; set; }

Then in MyExtension Package .cs I can get a DTEObject right after the package got initialized:然后在 MyExtension Package .cs 中,我可以在包初始化后立即获得 DTEObject:

protected override void Initialize ()
{
  MyExtension.Initialize (this);
  base.Initialize ();
  MyExtension.Instance.DTEObject = (EnvDTE.DTE)base.GetService (typeof (Microsoft.VisualStudio.Shell.Interop.SDTE));
}

Now I can work with the DTEObject within the extension and get any Object via GetObject.现在我可以在扩展中使用 DTEObject 并通过 GetObject 获取任何对象。 In My case I'm getting the current instance of the VersionControlEx.在我的情况下,我正在获取 VersionControlEx 的当前实例。

var tfsVersionControlExt = DTEObject.GetObject ("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt;

And then I'm able to get the VersionControlServer for this instance:然后我就可以得到这个实例的 VersionControlServer:

TfsVersionControlServer = tfsVersionControlExt.Explorer.Workspace.VersionControlServer;

The drawback here is, that the new instance of Visual Studio needs to have the Source Control Explorer to be opened.这里的缺点是,Visual Studio 的新实例需要打开源代码管理资源管理器。 If the user starts his Visual Studio without the Explorer, you will always getting null for the VersioncontrolServer.如果用户在没有资源管理器的情况下启动他的 Visual Studio,您将始终为 VersioncontrolServer 获取空值。

The workaround I did is, that I'm waiting for the Studio to startup and then I navigate in the Source Control Explorer (programatically) to the base path and force the Studio to load the Explorer and then I'm able to get what I need.我所做的解决方法是,我正在等待 Studio 启动,然后在源代码管理资源管理器中(以编程方式)导航到基本路径并强制 Studio 加载资源管理器,然后我就可以得到我想要的需要。 I'm not that happy with my solution, but i go it working.我对我的解决方案不太满意,但我继续努力。 If there is a better solution, I'm listening :)如果有更好的解决方案,我在听:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM