英文:
EA Addin EA_OnContextItemModified
问题
以下是翻译好的部分:
我在C#中创建了一个插件。我想阻止具有“已批准”状态的元素,直到该状态从“已批准”更改为止,以便不会修改/更新任何字段。
我尝试了下面的API,从方法签名中我理解将在之后获取更新,但没有找到任何用于获取预事件的API/方法(类似于EA_OnPreContextItemModified)。
public virtual void EA_OnContextItemModified(Repository repository, string guid, ObjectType ot)
{
if (ot == ObjectType.otElement && repository != null)
{
var element = repository.GetElementByGuid(guid);
if (element.Status == "Approved")
{
throw new Exception("无法修改此元素,因为它已获批准!请在修改此元素之前更改状态!");
}
}
}
例如,使用上述API,如果元素的状态为“已实施”,用户更改为“已批准”,那么在API内部,我将得到状态“已批准”,并抛出异常,以阻止数据库使用新更新进行更新。在这种情况下,我想获取具有“已实施”状态的对象的预事件状态,以及将发生的更改,然后我可以决定是否允许。
英文:
I created an addin in C#. I want to prevent elements that have Status "Approved" so that no field is modified/updated until that Status is changed from "Approved".
I tried with API below, from the method signature I understood that will get updates after, but didn't found any API/method to get a pre-event. (something like EA_OnPreContextItemModified)
public virtual void EA_OnContextItemModified(Repository repository, string guid, ObjectType ot)
{
if (ot == ObjectType.otElement && repository != null)
{
var element = repository.GetElementByGuid(guid);
if (element.Status == "Approved")
{
throw new Exception("Cannot modify this element because is Approved! Please change the status before modify this element!");
}
}
}
For example with the API above if the element has status "Implemented" and user changes to "Approved", then inside the API I get status "Approved" and throws and exception to not let the database to update with new update. In this scenario I want to get the pre-event status of the object with status "Implemented" together with what will change and me to take the decision to allow or not, is there a way?
答案1
得分: 1
你必须使用事件EA_OnContextItemChanged
来记住元素的最后状态。
然后将其与EA_OnContextItemModified
中的元素状态进行比较。
有点像下面的(VBScript)代码:
Dim contextName
Dim contextGUID
function EA_OnContextItemChanged(GUID, ot)
if ot = otElement then
Dim contextElement
set contextElement = Repository.GetElementByGuid(GUID)
if not contextElement is nothing then
contextName = contextElement.Name
contextGUID = contextElement.ElementGUID
end if
end if
end function
function EA_OnNotifyContextItemModified(GUID, ot)
'检查名称是否已更改
if GUID = contextGUID then
Dim contextElement
set contextElement = Repository.GetElementByGuid(GUID)
if contextName <> contextElement.Name then
msgbox "Element with name '" & contextName & "' has been changed to '" & contextElement.Name & "'"
end if
end if
end function
API没有提供停止更改的方法,所以你唯一能做的就是通过用你记住的值覆盖它来撤消它。
英文:
You have to use the event EA_OnContextItemChanged
, to remember the last status of the element.
Then compare it to the status of the element in EA_OnContextItemModified
.
A bit like the (VBScript) code below:
Dim contextName
Dim contextGUID
function EA_OnContextItemChanged(GUID, ot)
if ot = otElement then
Dim contextElement
set contextElement = Repository.GetElementByGuid(GUID)
if not contextElement is nothing then
contextName = contextElement.Name
contextGUID = contextElement.ElementGUID
end if
end if
end function
function EA_OnNotifyContextItemModified(GUID, ot)
'check if the name has been changed
if GUID = contextGUID then
Dim contextElement
set contextElement = Repository.GetElementByGuid(GUID)
if contextName <> contextElement.Name then
msgbox "Element with name '" & contextName & "' has been changed to '" & contextElement.Name & "'"
end if
end if
end function
The API doesn't offer a way to stop the change, so all you can do it undo it by overwriting it with the values you remembered.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论