问题
I am generating a custom workflow diagram via DGML API where each node corresponds to a C# class. I would like to be able to use the built-in 'Go To Definition' feature but the documentation is lacking.
回答1:
If you know the class´s filename and the position of the symbol definition, you can use the VsShellUtilities
class to open the document and scroll the code artifact into the view (by setting the caret position). In one of my extensions I do something like this...
If have a SourceInfo
type which I use to store the filename and text-range...
void GotoDefinition(
IServiceProvider serviceProvider,
SourceInfo source)
{
IVsUIHierarchy hierarchy;
uint itemId;
IVsWindowFrame windowFrame;
IVsTextView view;
VsShellUtilities.OpenDocument(
serviceProvider,
source.Filename,
Guid.Empty,
out hierarchy,
out itemId,
out windowFrame,
out view);
if (view != null)
{
int line, column;
int pos = source.TextRange.Start;
if (view.GetLineAndColumn(pos, out line, out column) == VSConstants.S_OK)
{
view.SetCaretPos(line, column);
view.CenterLines(line, 1);
}
}
}
class SourceInfo
{
public string Filename { get; set; }
public TextRange TextRange { get; set; }
}
回答2:
You cannot modify goto definition, but you can use "goto reference" instead. If you manually edit the DGML file in a text editor you can add a "Reference" property to a node, like this:
<Node Id="Boomerang" Reference="Boomerang.dgml"/>
Then when you right click this node in VS you will see a new menu appear named "Go To Reference" with a submenu containing "Reference", if you click this it will open the referenced DGML file.
See https://msdn.microsoft.com/en-us/library/ee842619.aspx#AddReferences for more detail.
回答3:
Visual studio have it's own property "SourceLocation"
You should declare it in properties
<Properties>
...
<Property Id="SourceLocation" Label="Start Line Number" DataType="Microsoft.VisualStudio.GraphModel.CodeSchema.SourceLocation" />
...
</Properties>
then use it inside Node element f.e.
<Node Id="class1" Label="FirstClass" SourceLocation="(Assembly=file:///D:/Prj/TestApp/AppConsole/Program.cs StartLineNumber=8 StartCharacterOffset=1 EndLineNumber=8 EndCharacterOffset=1)"/>
来源:https://stackoverflow.com/questions/28497753/dgml-how-to-enable-the-go-to-definition-for-custom-diagrams