1 ضمیمه
ایجاد خصوصیت Collection برای Component سفارشی
با سلام.
من در ساخت ایجاد Collection برای اشیاء سفارشی دانش کافی ندارم و زمانی که در دیزاین مقادیر خصوصیت تغییر می کنه ثبت می شه اما زمانی که قراره اون مقادیر دوباره استفاده بشه دیزاینر خطا بر می گردونه:
خصوصیتی که برای شیء سفارشی ساختم اینه:
Private VariablesValue As New List(Of VariableItem)
<DesignerSerializationVisibility(DesignerSerializa tionVisibility.Visible)> _
Public Property Variables() As List(Of VariableItem)
Get
Return VariablesValue
End Get
Set(value As List(Of VariableItem))
VariablesValue = value
End Set
End Property
کلاس آیتم های لیست هم این هست:
Imports System.ComponentModel
<Serializable()> _
Public Class VariableItem
Sub New()
MyBase.New()
Me.NameValue = MyBase.MemberwiseClone.Name
End Sub
Sub New(Item As VariableItem)
MyBase.New()
If String.IsNullOrEmpty(Item.Name) Then
Me.NameValue = MyBase.MemberwiseClone.Name
Else
Me.NameValue = Item.Name
End If
Me.ValueValue = Item.Value
End Sub
Sub New(Name As String, Value As String)
MyBase.New()
If String.IsNullOrEmpty(Name) Then
Me.NameValue = MyBase.MemberwiseClone.Name
Else
Me.NameValue = Name
End If
Me.NameValue = Name
Me.ValueValue = Value
End Sub
Private NameValue As String
<DesignerSerializationVisibility(DesignerSerializa tionVisibility.Visible)> _
<ParenthesizePropertyName(True)> _
Public Property Name() As String
Get
Return NameValue
End Get
Set(ByVal value As String)
NameValue = value
End Set
End Property
Private ValueValue As String
<DesignerSerializationVisibility(DesignerSerializa tionVisibility.Visible)> _
Public Property Value() As String
Get
Return ValueValue
End Get
Set(ByVal value As String)
ValueValue = value
End Set
End Property
End Class
و اما خطا:
ضمیمه 155567
|
|
Instances of this error (1) |
|
|
at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.RtFieldInfo.UnsafeSetValue(Objec t obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
at System.Runtime.Serialization.FormatterServices.Ser ializationSetValue(MemberInfo fi, Object target, Object value)
at System.Runtime.Serialization.ObjectManager.Complet eObject(ObjectHolder holder, Boolean bObjectFullyComplete)
at System.Runtime.Serialization.ObjectManager.DoNewly RegisteredObjectFixups(ObjectHolder holder)
at System.Runtime.Serialization.ObjectManager.Registe rObject(Object obj, Int64 objectID, SerializationInfo info, Int64 idOfContainingObj, MemberInfo member, Int32[] arrayIndex)
at System.Runtime.Serialization.Formatters.Binary.Obj ectReader.RegisterObject(Object obj, ParseRecord pr, ParseRecord objectPr, Boolean bIsString)
at System.Runtime.Serialization.Formatters.Binary.Obj ectReader.ParseObjectEnd(ParseRecord pr)
at System.Runtime.Serialization.Formatters.Binary.Obj ectReader.Parse(ParseRecord pr)
at System.Runtime.Serialization.Formatters.Binary.__B inaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.Obj ectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter.Deserialize(Stream serializationStream)
at System.Resources.ResXDataNode.GenerateObjectFromDa taNodeInfo(DataNodeInfo dataNodeInfo, ITypeResolutionService typeResolver)
at System.Resources.ResXDataNode.GetValue(ITypeResolu tionService typeResolver)
at System.Resources.ResXResourceReader.ParseDataNode( XmlTextReader reader, Boolean isMetaData)
at System.Resources.ResXResourceReader.ParseXml(XmlTe xtReader reader) |
|
|
منتظر راهنمایی های سبزتان هستیم ...
با تشکر.
نقل قول: ایجاد خصوصیت Collection برای Component سفارشی
من از کلاس سفارشی هم برای این Collection استفاده کردم ولی در این مورد حتی اجازه اضافه کردن آیتم به خصوصیت هم نمی دهد:
Imports System.ComponentModel
<Serializable(), TypeConverter(GetType(VariableCollectionConverter) )> _
Public Class VariableCollection
Inherits List(Of VariableItem)
Overloads Sub Add(Name As String, Value As String)
MyBase.Add(New VariableItem(Name, Value))
End Sub
Overloads Sub Add(Item As VariableItem)
MyBase.Add(Item)
End Sub
End Class
Imports System.Reflection
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Collections.Generic
Public Class VariableCollectionConverter
Inherits TypeConverter
Public Overrides Function ConvertFrom(context As System.ComponentModel.ITypeDescriptorContext, culture As System.Globalization.CultureInfo, value As Object) As Object
If TypeOf (value) Is String Then
Dim Coll As New VariableCollection
Dim Expr As String = value
Dim Items As String() = Expr.Split("/")
Dim NV As String() = {}
Dim v As VariableItem
For Each Item As String In Items
NV = Item.Split(":")
v = New VariableItem(NV(0), NV(1))
Coll.Add(v)
Next
Return Coll
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
Public Overrides Function ConvertTo(context As System.ComponentModel.ITypeDescriptorContext, culture As System.Globalization.CultureInfo, value As Object, destinationType As System.Type) As Object
If destinationType Is GetType(String) Then
Dim Expr As String = ""
With DirectCast(value, VariableCollection)
For i = 0 To .Count - 1
Expr += String.Format("{0}:{1}/", .Item(i).Name, .Item(i).Value)
Next
End With
If Expr.Length = 0 Then Return Expr
Return Expr.Remove(Expr.Length - 1, 1)
ElseIf destinationType Is GetType(InstanceDescriptor) Then
Dim ci As ConstructorInfo = GetType(VariableCollection).GetConstructor(New Type() {GetType(List(Of VariableItem))})
Dim t As VariableCollection = CType(value, VariableCollection)
Return New InstanceDescriptor(ci, t.ToArray)
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
Public Overrides Function CanConvertTo(context As System.ComponentModel.ITypeDescriptorContext, destinationType As System.Type) As Boolean
If destinationType Is GetType(String) Or destinationType Is GetType(InstanceDescriptor) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationType)
End Function
End Class
1 ضمیمه
نقل قول: ایجاد خصوصیت Collection برای Component سفارشی
جواب رو پیدا کردم :
فعلا که خطای دیزاین برنمی گرده
ضمیمه 155568
باید مقدار DesignerSerialzationVisiblity برابر با Content می شد....
<DesignerSerializationVisibility(DesignerSerializa tionVisibility.Content)> _
https://barnamenevis.org/showthread....l=1#post676850
البته هنوز نمی شه برای این خصوصیت کلاس سفارشی که یک Collection سفارشی باشه مثل این کلاس استفاده کرد: !!!!
Imports System.ComponentModel
<Serializable()> _
Public Class VariableCollection
Inherits Collections.ObjectModel.Collection(Of VariableItem)
Public Sub New()
MyBase.New()
End Sub
Public Sub New(list As Collections.Generic.List(Of VariableItem))
MyBase.New(list)
End Sub
Public Overloads Sub Add(Name As String, Value As String)
MyBase.Add(New VariableItem(Name, Value))
End Sub
Public Overloads Sub Add(Item As VariableItem)
MyBase.Add(Item)
End Sub
Default Public Overloads Property Item(Name As String) As String
Get
For Each Var As VariableItem In MyBase.Items
If Var.Name.Equals(Name, StringComparison.OrdinalIgnoreCase) Then
Return Var.Value
End If
Next
Return String.Empty
End Get
Set(value As String)
For Each Var As VariableItem In MyBase.Items
If Var.Name.Equals(Name, StringComparison.OrdinalIgnoreCase) Then
Var.Value = value
End If
Next
End Set
End Property
End Class
فکر کنم باید براش یک Editor سفارشی ایجاد کرد.