Porady: pozyskiwanie elementów modelu UML z IDataObject
Gdy użytkownik przeciągnie elementy z dowolnego źródła na diagramie, przeciągane elementy zostaną zakodowane w System.Windows.Forms.IDataObject.Kodowanie jest zależne od typu obiektu źródłowego.Poniższy fragment przedstawia sposób pobierania elementów, gdy źródłem jest diagram UML.
[!UWAGA]
Większość operacji, które należy wykonać na modelach UML można przeprowadzić przy użyciu typów w zdefiniowanych zestawach Microsoft.VisualStudio.Uml.Interfaces i Microsoft.VisualStudio.ArchitectureTools.Extensibility.Ale w tym celu należy użyć niektórych klas, które są częścią implementacji narzędzia do modelowania UML.Na przykład, ShapeElement w tym fragmencie nie jest taki sam jak UML IShape.Aby zmniejszyć ryzyko wprowadzenia modelu UML i diagramów w niespójnym stanie, lepiej unikać stosowania metod na tych klasach implementacji, poza przypadkami, gdy nie ma żadnej alternatywy.
Przykład kodu
Twój profil musi odwoływać się do następujących .NET zestawów:
Microsoft.VisualStudio.Modeling.Sdk.12.0
Microsoft.VisualStudio.Modeling.Sdk.Diagrams.12.0
System.Windows.Forms
using Microsoft.VisualStudio.Modeling;
// for ElementGroupPrototype
using Microsoft.VisualStudio.Modeling.Diagrams;
// for ShapeElement, DiagramDragEventArgs, DiagramPointEventArgs
…
/// <summary>
/// Retrieves UML IElements from drag arguments.
/// Works for drags from UML diagrams.
/// </summary>
private IEnumerable<IElement> GetModelElementsFromDragEvent
(DiagramDragEventArgs dragEvent)
{
//ElementGroupPrototype is the container for
//dragged and copied elements and toolbox items.
ElementGroupPrototype prototype =
dragEvent.Data.
GetData(typeof(ElementGroupPrototype))
as ElementGroupPrototype;
// Locate the originals in the implementation store.
IElementDirectory implementationDirectory =
dragEvent.DiagramClientView.Diagram.Store.ElementDirectory;
return prototype.ProtoElements.Select(
prototypeElement =>
{
ModelElement element = implementationDirectory
.FindElement(prototypeElement.ElementId);
ShapeElement shapeElement = element as ShapeElement;
if (shapeElement != null)
{
// Dragged from a diagram.
return shapeElement.ModelElement as IElement;
}
else
{
// Dragged from UML Model Explorer.
return element as IElement;
}
});
}
Aby uzyskać więcej informacji o ElementGroupPrototype i Store, w których realizowane są narzędzia modelowania UML, zobacz Modelowanie SDK dla Visual Studio — języki specyficzne dla domeny.
Zobacz też
Koncepcje
Programowanie za pomocą API UML
Porady: definiowanie polecenia menu na diagramie modelowania