问题
I'm adding some options for the Connection Node in the server explorer for my VS extension. For one of the menu options that I have added I need to get the connection string of the very selected connection, So I have tried this by adding some code in the BeforeQueryStatus event and also in the command handler method. But looks like I'm missing something since every time I want to get an Instance of the DataViewHierarchyAccessor I always get a null value. Although if I do the same thing but in a lower level node works fine. I have used the following code with no luck:
IVsDataExplorerNodeSelection nodeSelection = (IVsDataExplorerNodeSelection)Package.GetGlobalService(typeof(IVsDataExplorerNodeSelection));
IVsUIHierarchy hierarchy = Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsUIHierarchy)) as IVsUIHierarchy;
Has anybody done something similar for the Connection Node of the Server Explorer Window?
Update:
Looks like I got some progress on this, I just tried this:
EnvDTE80.DTE2 _applicationObject = GetDTE2();
UIHierarchy uih = _applicationObject.ToolWindows.GetToolWindow("Server Explorer") as UIHierarchy;
Array selectedItems = (Array)uih.SelectedItems;
if (null != selectedItems)
{
foreach (UIHierarchyItem selItem in selectedItems)
{
SelectedItem prjItem = selItem.Object as EnvDTE.SelectedItem;
string name = prjItem.Name;
}
}
Now my question is if there's a better type to cast the list of selectedItems? instead of EnvDTE.SelectedItem.
Thanks in advance.
回答1:
Marking my question as answered since the code that I posted did the trick.
EnvDTE80.DTE2 _applicationObject = GetDTE2();
UIHierarchy uih = _applicationObject.ToolWindows.GetToolWindow("Server Explorer") as UIHierarchy;
Array selectedItems = (Array)uih.SelectedItems;
if (null != selectedItems)
{
foreach (UIHierarchyItem selItem in selectedItems)
{
SelectedItem prjItem = selItem.Object as EnvDTE.SelectedItem;
string name = prjItem.Name;
}
}
来源:https://stackoverflow.com/questions/15541438/how-to-get-the-selected-connection-node-object-of-vs-server-explorer-window-ddex