我目前正在开发VS扩展程序/加载项,我需要将工作项与挂起的更改相关联(不会触发签入)。
搜索了几个小时后,我无法通过API找到实现此目的的方法。我发现将工作项与挂起的更改关联的唯一方法是Workspace.CheckIn(),它也会触发签入..
我错过了什么吗?或者这真的不可能吗?
答案 0 :(得分:2)
我在下面提供了示例代码,请注意,我还没有尝试过这段代码,但似乎只能通过一些反思来实现,因为在API中没有公共方法可以实现这一点。使用VS2012 / 13中的新Team Explorer窗口,您最好扩展Team Explorer以提供您想要的功能。在MSDN上有一些examples扩展它。
下面的代码,获取服务提供者实例。您可以通过包实例获取IServiceProvider
对象。一旦得到它,您需要调用私有的AddWorkItemById方法 - 检查方法定义here。
int id = workItemId;
IPendingChangesExt service = serviceProvider.GetService<IPendingChangesExt>();
FieldInfo field = service.GetType().GetField("m_workItemsSection", BindingFlags.Instance | BindingFlags.NonPublic);
Type fieldType = field.FieldType;
object value = field.GetValue(service);
MethodInfo method = fieldType.GetMethod("AddWorkItemById", BindingFlags.Instance | BindingFlags.NonPublic);
object[] objArray = new object[] { id };
method.Invoke(value, objArray);
答案 1 :(得分:2)
很抱歉我迟到了,非常感谢你。我不得不稍微修改它以使其正常工作:
// VersionControlExt is needed
var dte = Package.GetGlobalService(typeof(DTE)) as DTE;
var dte2 = (DTE2)dte;
var vce = dte2.DTE.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt")
as VersionControlExt;
var pendingChangesExtField = vce.PendingChanges.GetType().GetField("m_pendingChangesExt", BindingFlags.Instance | BindingFlags.NonPublic);
var pendingChangesExt = pendingChangesExtField.GetValue(vce.PendingChanges);
// pendingChangesExt is null when the Pending Changes Window isn't opened
if (pendingChangesExt == null)
return;
var workItemSectionField = pendingChangesExt.GetType().GetField("m_workItemsSection", BindingFlags.Instance | BindingFlags.NonPublic);
var workItemSection = workItemSectionField.GetValue(pendingChangesExt);
// Assign new Work Item to Pending Changes
var addMethod = workItemSectionField.FieldType.GetMethod("AddWorkItemById", BindingFlags.Instance | BindingFlags.NonPublic);
object[] addArray = { id };
addMethod.Invoke(workItemSection, addArray);
如果有人对如何实施拖放感兴趣挂起更改窗口的“相关工作项”部分:
versionControlServer = ServiceProvider.GetService<ITeamFoundationContextManager>().TeamProjectCollection.GetService<VersionControlServer>();
var selectedItems = new[] {1, 2};
var dropData = new WorkItemDropData(versionControlServer.ServerGuid, selectedItems);
var dataObject = new DataObject("Microsoft.TeamFoundation.WorkItemId", dropData);
DragDrop.DoDragDrop(listView, dataObject, DragDropEffects.Move);