gpt4 book ai didi

c# - 为什么我不能在 WebForm 中声明 UserControl 的子元素(属性)?

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:08 24 4
gpt4 key购买 nike

我有三个类和一个将使用这三个类的用户控件。
以下是类及其解释:

//provides access to multiple ManagementMethods
[Serializable(), ParseChildren(true)]
public class ManagementDelegate
{
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),
PersistenceMode(PersistenceMode.InnerProperty)]
public List<ManagementMethod> 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<ManagementParameter> 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;
}

下面是我正在寻找的结构示例:

<UC:MyUc ID="test" runat="server">
<SelectMethods>
<!-- here when i open a tag asp.net lists the Method, but when i try to set the Name attribute it warns and won't run. -->
<Method Name="meth">
<Parameter ParameterName="id" />
<Parameter ParameterName="word" />
</Method
<Method Name="meth2">
</Method
</SelectMethods>
</UC:MyUc>

问题是 ASP.net 将 SelectMethod 识别为内部标记,它甚至将 Method 标记识别为内部标记,但它不识别实际上是 ManagementMethod 的 Method 标记的类型。当我将任何属性的类型更改为简单类型时,例如将列表更改为 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<ControlDependency> 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<ControlDependency> ControlDependencies { get; set; }

**Code Emitted

}

并在自定义控件中声明为

[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerProperty),
NotifyParentProperty(true)]
public List<ControlDependencySetting> ControlDependencySettings { get; set; }

另请注意,我确实在每个构造函数中创建了这些列表的实例。

*EDIT***

您已经调用了您的列表 Method 和您的元素 Method ,您希望 ManagementDelegate 的子元素自动添加到名为的列表中方法,这不是它的工作原理,当你想添加到列表时,你需要指定列表元素并在其中添加,你对参数也做了同样的事情。

这是您当前结构所期望的。

<SelectMethods> 
<Method> <--Now this is the list of methods, you have also called it method
<Method Name="meth"> <-- This is an element of the method type to add to the list you have called Method
<Parameter> <-- This is the list of parameters you have called it Parameter
<Parameter ParameterName="id" /> <--parameter element
<Parameter ParameterName="word" />
<Parameter>
</Method>
</Method>

您应该重命名为 MethodsParameters 并相应地使用或重新构建您的布局。

有点像

    //provides access to multiple ManagementMethods
[Serializable(), ParseChildren(true)]
public class ManagementDelegate
{
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always),
PersistenceMode(PersistenceMode.InnerProperty)]
public List<ManagementMethod> 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<ManagementParameter> 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;
}

然后

<UC:MyUc ID="test" runat="server">
<SelectDelegate>
<Methods>
<Method Name="meth">
<Parameters>
<Parameter ParameterName="id" />
<Parameter ParameterName="word" />
<Parameters>
</Method>
<Method Name="meth2">
</Method>
</Methods>
</SelectDelegate>
</UC:MyUc>

我相信也可以指定默认情况下内部内容属于特定属性,请参阅 PersistenceMode.InnerDefaultProperty - 但我从未尝试过。

关于c# - 为什么我不能在 WebForm 中声明 UserControl 的子元素(属性)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8743680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com