为什么我不能在WebForm中声明UserControl的子元素(属性)?
我有三个类和一个将使用这三个类的用户控件。
以下是课程及其解释:
//provides access to multiple ManagementMethods [Serializable(), ParseChildren(true)] public class ManagementDelegate { [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), PersistenceMode(PersistenceMode.InnerProperty)] public List Method { get; set; } } //provides access to multiple ManagementParameters and the method name [Serializable(), PersistChildren(false)] public class ManagementMethod { [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] public string Name { get; set; } [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), PersistenceMode(PersistenceMode.InnerProperty)] public List Parameter { get; set; } } //describes a parameter of method. [Serializable(), PersistChildren(false)] public class ManagementParameter { [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] public string ParameterName { get; set; } } //=============================== //here is the part of user control code behind that uses the ManagementDelegate class. [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), PersistenceMode(PersistenceMode.InnerProperty)] public ManagementDelegate SelectMethods { get; set; }
所以这是我正在寻找的结构的一个例子:
</Method </Method
问题是ASP.net将SelectMethod识别为内部标记,它甚至将Method标记识别为内部标记,但它不识别实际为ManagementMethod的Method标记的类型。 当我将任何属性的类型更改为简单类型时,例如将List更改为ManagementMethod,ASP.net会识别它并且一切正常。 你也可以使用任何List 对象。
这是我们的生产代码的片段,您可以看到ControlDependency
类甚至允许ControlDependency
子代
[PersistChildren(false), TypeConverter(typeof(ExpandableObjectConverter)), ParseChildren(true), Serializable()] public class ControlDependencySetting { [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty)] public List ControlDependencies { get; set; } ***Code emitted } [PersistChildren(false), TypeConverter(typeof(ExpandableObjectConverter)), ParseChildren(true),Serializable()] public class ControlDependency { [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty)] public List ControlDependencies { get; set; } **Code Emitted }
并在自定义控件中声明为
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content), PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(true)] public List ControlDependencySettings { get; set; }
另请注意,我确实在每个构造函数中创建了这些列表的实例。
*编辑***
您调用了列表Method
和元素Method
,您希望ManagementDelegate
子元素自动添加到名为Method
的列表中,这不是它的工作原理,当您想要添加到列表时需要指定列表元素并在其中添加,你已经为参数做了同样的事情。
这是您目前的结构所期望的。
<--Now this is the list of methods, you have also called it method <-- This is an element of the method type to add to the list you have called Method <-- This is the list of parameters you have called it Parameter <--parameter element
您应该重命名为Methods
和Parameters
并相应地使用或重新构建您的布局。
就像是
//provides access to multiple ManagementMethods [Serializable(), ParseChildren(true)] public class ManagementDelegate { [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), PersistenceMode(PersistenceMode.InnerProperty)] public List Methods { get; set; } } //provides access to multiple ManagementParameters and the method name [Serializable(), PersistChildren(false),TypeConverter(typeof(ExpandableObjectConverter))] public class ManagementMethod { [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] public string Name { get; set; } [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), PersistenceMode(PersistenceMode.InnerProperty)] public List Parameters { get; set; } } //describes a parameter of method. [Serializable(), PersistChildren(false),TypeConverter(typeof(ExpandableObjectConverter))] public class ManagementParameter { [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] public string ParameterName { get; set; } } //=============================== //here is the part of user control code behind that uses the ManagementDelegate class. [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), PersistenceMode(PersistenceMode.InnerProperty)] public ManagementDelegate SelectDelegate { get; set; }
然后
我相信默认情况下也可能会指出内部属性属于特定属性,请参阅PersistenceMode.InnerDefaultProperty – 但我从未尝试过这个。