当我的图标装饰器位于我的形状之外时,为什么在 InitializeShapeFields 中找不到它们?

Why are my icon decorators not being found in InitializeShapeFields when they positioned outside my shape?

我创建了一个在双击时执行事件的自定义图像字段,并且我正在按照文档所述替换 InitializeShapeFields 中的图标。

当我将图标放在形状 (InnerMiddleRight) 内时,我可以双击它,但是当我将它放在外面 (OuterTopRight) 时,在 InitializeShapeFields() 中找不到装饰器,我无法双击它。当我点击它时,它会突出显示父形状和装饰器,因此它正在识别 link.

关于装饰器上的交互事件,我是否遗漏了什么?

 protected override void InitializeShapeFields(IList<ShapeField> shapeFields)
    {
        base.InitializeShapeFields(shapeFields);           

        ShapeField editCodeField = shapeFields.FirstOrDefault(f => f.Name == "EditCodeDecorator");

        if (editCodeField == null)
            return;

        shapeFields.Remove(editCodeField);

        ImageField editCodeImage = new ShowCodeImageField("EditCodeDecorator")
        {
            DefaultImage = ImageHelper.GetImage(
                PFlowDomainModel.SingletonResourceManager.GetObject("BaseShapeEditCodeDecoratorDefaultImage"))
        };
        shapeFields.Add(editCodeImage);
    }

internal class ShowCodeImageField : ImageField
{
    public static BuildCodeEvent InteractionEvents = new BuildCodeEvent();

    public ShowCodeImageField(string fieldName) : base(fieldName)
    {
    }

    public ShowCodeImageField(string fieldName, Image image) : base(fieldName, image)
    {
    }

    public override void OnDoubleClick(DiagramPointEventArgs e)
    {
        base.OnDoubleClick(e);

        BaseShape shapeHit = e.HitDiagramItem.Shape as BaseShape;

        if (shapeHit != null)
        {
            InteractionEvents.OnRegisterFileBuildEvent(shapeHit);

            e.Handled = true;
        }
    }
}

所以我在查看生成的代码并观察对象的一些属性后解决了我的问题。

这在生成代码中泄露了它:

/// <summary>
    /// Initialize the collection of decorators associated with this shape type.  This method also
    /// creates shape fields for outer decorators, because these are not part of the shape fields collection
    /// associated with the shape, so they must be created here rather than in InitializeShapeFields.
    /// </summary>

因此,要双击形状外的图标装饰器,您需要将 InitialiseDecorators() 中的装饰器替换为具有自定义图像字段的新装饰器,然后不要使用 hit e.HitDiagramItem.Shape.

这是我的解决方案:

protected override void InitializeDecorators(IList<ShapeField> shapeFields, IList<Decorator> decorators)
    {
        base.InitializeDecorators(shapeFields, decorators);

        var oldDecorator = decorators.FirstOrDefault(d => d.Field.Name == "EditCodeDecorator");            

        if (oldDecorator == null)
            return;

        decorators.Remove(oldDecorator);

        ImageField editCodeImageField = new ShowCodeImageField("EditCodeDecorator")
        {
            DefaultImage = ImageHelper.GetImage(
                PFlowDomainModel.SingletonResourceManager.GetObject("BaseShapeEditCodeDecoratorDefaultImage"))
        };

        var newDecorator = new ShapeDecorator(editCodeImageField, ShapeDecoratorPosition.OuterTopRight, new PointD(-0.1, -0.1));
        decorators.Add(newDecorator);
    }

public override void OnDoubleClick(DiagramPointEventArgs e)
    {
        base.OnDoubleClick(e);

        var decoratorHostShape = e.HitDiagramItem.RepresentedElements.Cast<DecoratorHostShape>().First();

        var shape = decoratorHostShape.ParentShape;         

        if (shape != null)
        {
            InteractionEvents.OnRegisterFileBuildEvent(shape);

            e.Handled = true;
        }
    }

希望这可以帮助那里的人,如果有人知道更好的方法请在下面告诉我...