首页 > 解决方案 > 无法获取 Events.CacheAttached 以识别 Acuamtica 中的自定义属性

问题描述

这是我在此处发布的问题的后续行动 Override action on curyOrigDiscAmt

按照那里的建议,我正在尝试:

  1. 基于原始的 TermsAttribute 类创建自定义属性。

  2. 覆盖字段 APInvoice.TermsID

  3. 使用自定义属性装饰字段 APInvoice.TermsID 并使用它来替换原始条款。

  4. 最后改变 CalcDisc() 方法的逻辑

这是我所做的:

1.自定义属性:

public class CTERMSIDAttribute : PX.Objects.CS.TermsAttribute
{
    public CTERMSIDAttribute(Type DocDate, Type DueDate, Type DiscDate, Type CuryDocBal, Type CuryDiscBal) : base(DocDate, DueDate, DiscDate, CuryDocBal, CuryDiscBal)
    {
        var cBalance = _CuryDocBal;
        var cDiscount = _CuryDiscBal;
        int i = 0;
    }

    protected override void CalcDisc(PXCache sender, PXFieldUpdatedEventArgs e)
    {
        base.CalcDisc(sender, e);
        int i2 = 0;
        
    }
}

注意:目前没有任何事情要做。我只是在 int i = 0 上放了一个断点,以确保我的代码在运行时被执行。

我还创建了一个用新创建的属性装饰的类:

public class CTermID : Vendor.termsID
{
    [PXDBString(10, IsUnicode = true)]
    [PXDefault(typeof(Search<Vendor.termsID,
        Where<Vendor.bAccountID, Equal<Current<APInvoice.vendorID>>,
            And<Current<APInvoice.docType>, NotEqual<APDocType.debitAdj>>>>),
        PersistingCheck = PXPersistingCheck.Nothing)]
    [PXUIField(DisplayName = "Terms", Visibility = PXUIVisibility.Visible)]
    [APTermsSelector]
    [CTERMSIDAttribute(typeof(APInvoice.docDate), typeof(APInvoice.dueDate), typeof(APInvoice.discDate),
           typeof(APInvoice.curyOrigDocAmt), typeof(APInvoice.curyOrigDiscAmt))]
    public string termsID   { get;  set; }
}

我不确定这是否需要或使用,但我在一个片段中看到它,所以我实现了它。

2 & 3. 覆盖和装饰

    [PXMergeAttributes(Method = MergeMethod.Append)] // add to other properties 
    [PXRemoveBaseAttribute(typeof(Vendor.termsID))]  // remove existing property
    [CTERMSIDAttribute(typeof(APInvoice.docDate), typeof(APInvoice.dueDate), typeof(APInvoice.discDate),
               typeof(APInvoice.curyOrigDocAmt), typeof(APInvoice.curyOrigDiscAmt))]
    public CTermID termsID { get; set; }   // redefine termsID to use custom attribute
    protected virtual void _(Events.CacheAttached<APInvoice.termsID> e)
    {
        int i3 = 0;
    }

4. CalcDisc() 的重写方法

这是在新属性中(之前在步骤 1 中显示):

    protected override void CalcDisc(PXCache sender, PXFieldUpdatedEventArgs e)
    {
        base.CalcDisc(sender, e);
        int i2 = 0;            
    }

同样,它现在什么也不做,我只是在 int i2 = 0 处放置一个断点来检查它是否在运行时命中。事实并非如此。

发生的情况是第一个断点(i = 0)命中......实际上它命中了好几次......但其他断点都没有,这告诉我覆盖工作不正常。

我错过了什么?

[编辑] 根据 Patrick Chen 的建议,我将其更改为:

[PXMergeAttributes(Method = MergeMethod.Replace)]
[CTERMSIDAttribute(typeof(APInvoice.docDate), typeof(APInvoice.dueDate), typeof(APInvoice.discDate), typeof(APInvoice.curyOrigDocAmt), typeof(APInvoice.curyOrigDiscAmt))]
public CTermID termsID { get; set; }   // redefine termsID to use custom attribute
protected virtual void _(Events.CacheAttached<APInvoice.termsID> e)
      {
        int i = 0;
      }

而且我的 DoCalc() 版本仍然没有被解雇。

标签: c#acumatica

解决方案


CalcDisc在您的自定义属性中没有调用方法的机制。

在事件处理程序中用于调用TermsAttribute方法: FieldUpdatedCalcDisc在此处输入图像描述

FieldUpdated方法不存在于自定义属性中,因此该方法不被任何机制调用。自定义属性必须更紧密地遵循具有大约十几种方法的原始实现。


推荐阅读