我遇到了ShouldSerialize功能的问题 我在其上定义了一个带有标签(名为Label1)的usercontrol 现在我将下面的代码放在usercontrol类中:
NSString *searchString = @"EVBAR02";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageUrl CONTAINS[cd] %@", searchString];
现在以新的形式包含usercontrol
在usercontrol的属性网格中,您可以找到TBValueRange属性
如果右键单击属性名称,则可以重新启动该属性
重新启动后,您可以在物业中看到值200
现在,重新生成项目会将属性重置为初始值(id 100)
为什么价值200没有留下来?
如果我替换函数ShouldSerializeTBValueRange()中的行NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageUrl CONTAINS[cd] 'EVBAR02'"];
不返回_Range.Equals(200)
它会起作用。 我不明白。
任何人都可以解释一下吗?
答案 0 :(得分:1)
这些方法按编码方式工作。 似乎不按预期工作的事情是Not _Range.Equals(200)
部分。
您将_Range
初始化为100:Dim _Range As UShort = 100
然后,当{em>不 ShouldSerializeTBValueRange
时,您的200
方法告诉VS 保存范围值:{{1 }}
因此,当您在IDE中将其重置为Return _Range <> 200
时,它不会保存200
的值,并显示200
的初始值。
代码使用一个值作为默认值100
,但为100
test使用不同的值。您应该将其更改为使用默认值200:
ShouldSerialize
或更改Dim _Range As UShort = 200
测试以使用ShouldSerialize
:
100
将默认值视为默认值,并且仅使用一个值作为默认值,只使用Public Function ShouldSerializeTBValueRange() As Boolean
Return _Range <> 100
End Function
和ShouldSerializexxx
按预期工作,而不需要任何其他内容。
Resetxxx
使用一个值作为实际默认值(100),然后根据不同值(200)返回T / F会导致
' THIS is the default value -
' ie the value that need not be saved because the
' control automatically starts with that value
Dim _Range As UShort = 200US
' Controls:
' - displays the prop value in Bold in the property window
' when the value is NOT the default
' - saves the value when it is NOT the default
' - enables/disables the RESET function
Public Function ShouldSerializeTBValueRange() As Boolean
Return (_Range <> 200US)
End Function
菜单项和粗体值处于错误状态奇怪的是Reset
似乎失败了。它在重置(返回200)后返回Not _Range.Equals(200)
,这会导致值在真正不应该保存时保存。这有两个重载:
False
如果您传递的不是实际的 UInt16.Equals(obj As Object) As Boolean
UInt16.Equals(v As UShort) As Boolean
,则该值将被设置为UInt16/UShort
。因此,Object
正在使用第一个,因为Not _Range.Equals(200)
是200
。该版本的ILCode是:
Int32
测试将无法通过第一次测试,因为public override bool Equals(object obj)
{
return obj is ushort && this == (ushort)obj;
}
包含obj
。如果您传递Int32
,它将起作用:
UShort
理解不同数据类型的所有充分理由,避免装箱(UShort.Equals(_Range, 200US)
'or:
_Range.Equals(200US)
)并永远设置As Object
。
答案 1 :(得分:0)
谢谢大家花时间在我的问题上
我找到了答案:当ShouldSerialize函数返回false时,属性值不会保存在文件Form1.designer.vb中,因此它获取原始值。
对于我需要的,ShouldSerialize函数应该总是返回true。
Public Function ShouldSerializeTBValueRange() As Boolean
Return true
End Function
因此将始终保存属性值 现在,我正在寻找的是如何控制属性网格的上下文菜单 我想要选择&#34; Reinit&#34;当价值好的时候会变灰。
答案 2 :(得分:0)
最后,我发现了一些东西,使用了PropertyDescriptor。 对于谁感兴趣,我们可以在designer.vb文件中保存属性并测试要重置的值 这是我的程序,基于MSDN - Microsft的文章:
Imports System.ComponentModel
Imports System.Windows.Forms.Design
<Designer(GetType(DemoControlDesigner))>
Public Class UserControl1
Dim _MyProperty1 As String = "Test1"
Dim _MyProperty2 As Integer = 100
Dim _MyProperty3 As UShort = 200
Public Sub New()
InitializeComponent()
End Sub
Public Function CanResetMyProperty1() As Boolean
Return _MyProperty1 <> "Test"
End Function
Public Sub ResetMyProperty1()
_MyProperty1 = "Test"
End Sub
Public Property MyProperty1 As String
Get
Return _MyProperty1
End Get
Set(ByVal Value As String)
_MyProperty1 = Value
End Set
End Property
Public Function CanResetMyProperty2() As Boolean
Return _MyProperty2 <> 150
End Function
Public Sub ResetMyProperty2()
_MyProperty2 = 150
End Sub
Public Property MyProperty2 As Integer
Get
Return _MyProperty2
End Get
Set(ByVal Value As Integer)
_MyProperty2 = Value
End Set
End Property
Public Function CanResetMyProperty3() As Boolean
Return _MyProperty3 <> _MyProperty2
End Function
Public Sub ResetMyProperty3()
_MyProperty3 = _MyProperty2
End Sub
Public Property MyProperty3 As UShort
Get
Return _MyProperty3
End Get
Set(ByVal Value As UShort)
_MyProperty3 = Value
End Set
End Property
End Class
Friend NotInheritable Class SerializePropertyDescriptor
Inherits PropertyDescriptor
Private _pd As PropertyDescriptor = Nothing
Public Sub New(ByVal pd As PropertyDescriptor)
MyBase.New(pd)
_pd = pd
End Sub
Public Overrides ReadOnly Property Attributes() As AttributeCollection
Get
Return Me._pd.Attributes
End Get
End Property
Protected Overrides Sub FillAttributes(ByVal attributeList As IList)
MyBase.FillAttributes(attributeList)
End Sub
Public Overrides ReadOnly Property ComponentType() As Type
Get
Return Me._pd.ComponentType
End Get
End Property
Public Overrides ReadOnly Property Converter() As TypeConverter
Get
Return Me._pd.Converter
End Get
End Property
Public Overrides Function GetEditor(ByVal editorBaseType As Type) As Object
Return Me._pd.GetEditor(editorBaseType)
End Function
Public Overrides ReadOnly Property IsReadOnly() As Boolean
Get
Return Me._pd.IsReadOnly
End Get
End Property
Public Overrides ReadOnly Property PropertyType() As Type
Get
Return Me._pd.PropertyType
End Get
End Property
Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
Try
Return CallByName(component, "CanReset" & _pd.Name, CallType.Get, Nothing)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return False
End Function
Public Overrides Function GetValue(ByVal component As Object) As Object
Return Me._pd.GetValue(component)
End Function
Public Overrides Sub ResetValue(ByVal component As Object)
Me._pd.ResetValue(component)
End Sub
Public Overrides Sub SetValue(ByVal component As Object, ByVal val As Object)
Me._pd.SetValue(component, val)
End Sub
Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
Return True
End Function
End Class
Class DemoControlDesigner
Inherits ControlDesigner
Dim PropertiesToSerialize As String() = {"MyProperty1", "MyProperty2", "MyProperty3"}
Protected Overrides Sub PostFilterProperties(ByVal properties As IDictionary)
Dim original As PropertyDescriptor
For Each PropName As String In PropertiesToSerialize
If properties.Contains(PropName) Then
original = properties(PropName)
properties(PropName) = New SerializePropertyDescriptor(original)
End If
Next
MyBase.PostFilterProperties(properties)
End Sub
End Class
对于应该序列化并测试重置的每个属性,我们应该编写一个子“CanResetPropertyName”来测试要重置的值(参见示例)。
即使我们重新启动项目,价值仍然存在
它对我来说很好,也许它可以改进。
问候。