我想在窗口小部件管理员中单击“提交保存”按钮时访问内容部分中的selectedvalue(枚举字段)。我怎么能在编辑器方法中做到这一点?
如果您知道具有该字段的ContentPart的名称,您可以这样做:
(dynamic)contentItem.ContentPartName.FieldName.SelectedValue
但是,如果您不知道ContentPart的名称,则可以首先使用它来在运行时获取内容项的所有字段:
using System.Runtime.CompilerServices;
using Microsoft.CSharp.RuntimeBinder;
// get all the fields from the contentItem without knowing part name
var callSite = CallSite<Func<CallSite, object, object>>
.Create(Binder.GetMember(0, contentItem.ContentType,
((dynamic)contentItem).GetType(), new[] { CSharpArgumentInfo.Create(0, null) }));
var contentItemFields = ((callSite.Target(callSite, ((dynamic)contentItem))).Fields) as List<ContentField>;
拥有字段列表,您现在可以搜索所需的枚举字段,并获取所选值:
var yourField = (contentItemFields.FirstOrDefault(f => f.name == "YourField")) as EnumerationField;
var selectedValue = yourField.SelectedValue;